思路:枚举字符串,用一个优先队列来进行维护就可以了,就是利用了字母a的优先级肯定比aX高(X为任意大写字母)代码很容易看懂;
//#include<bits/stdc++.h>
#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
const int MAXN = 1008;
map<string, int> sHash;
int n, m, strN[MAXN][30];
string str;
struct myStr {
string ss;
int ssNum;
myStr(){}
myStr(string s, int n):ss(s), ssNum(n){}
bool operator < (const myStr &a)const {
return ssNum < a.ssNum;
}
};
priority_queue<myStr> myQue;
void dfs(string s) {
for(int i = 0; i < 26; i++) {
string tmp = s;
tmp += i+'A';
int sLen = tmp.length(), tmpN[30];
memset(tmpN, 0, sizeof(tmpN));
for(int j = 0; j < sLen; j++)
tmpN[tmp[j]-'A']++;
int resN = 0;
for(int j = 1; j <= m; j++) {
int t = 0;
for(; t < 26; t++)
if (tmpN[t] > strN[j][t]) break;
if (t == 26) resN++;
}
myQue.push(myStr(tmp, resN));
}
}
int main() {
//freopen("funny.in", "r", stdin);
//freopen("funny.out", "w", stdout);
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++) {
cin>>str;
sHash[str] = 1;
int l = str.length();
for(int j = 0; j < l; j++) {
int tmp = str[j]-'A';
strN[i][tmp]++;
}
}
dfs("");
int pN = 0;
while (pN < n) {
string s;
while (1) {
myStr aa = myQue.top();
myQue.pop();
s = aa.ss;
dfs(s);
if (!sHash[s]) break;
}
cout<<s<<endl;
pN++;
}
return 0;
}