AtCoder Beginner Contest 268 D Unique Usernamed
Unique Username
模拟 搜索
全排列模拟,直接搜索
#include <iostream>
#include <cstdio>
#include <map>
#include <string>
using namespace std;
const int maxn = 20;
map<string, int>mp;
string s[maxn];
int vis[maxn];
string ans = "";
int _maxx = 0, n, m;
void dfs(int now, int _, string snow)
{
if(now == n)
{
if(snow.size() >= 3 && mp.count(snow) == 0)
ans = snow;
return;
}
if(now == 0)
{
for(int i=0; i<n && ans.size() == 0; i++)
{
if(vis[i]) continue;
vis[i] = 1;
dfs(now + 1, _, snow + s[i]);
vis[i] = 0;
}
return;
}
for(int i=1; _maxx - _ - i >= n - 1 - now && ans.size() == 0; i++)
{
snow += "_";
for(int j=0; j<n && ans.size() == 0; j++)
{
if(vis[j]) continue;
vis[j] = 1;
dfs(now + 1, _ + i, snow + s[j]);
vis[j] = 0;
}
}
}
int main()
{
cin >> n >> m;
int sum = 0;
for(int i=0; i<n; i++)
{
cin >> s[i];
sum += s[i].size();
}
_maxx = 16 - sum;
for(int i=0; i<m; i++)
{
string a;
cin >> a;
mp[a] = 1;
}
dfs(0, 0, "");
if(ans.size() == 0) ans = "-1";
cout << ans << endl;
return 0;
}