题目大意:如果又一个字符串包含俩个相邻重复子串,则称之为容易的串,反之则是困难的串,给出m和n,找最多出现前n个字母组成的困难串表字典序排列的第m个困难串
解题思路:每次加入字母时都与已有字母串进行比较,找是否有相同连续的串,判断前一半是否等于后一半,等于则跳出,不等于则继续
#include <cstdio>
#include <iostream>
using namespace std;
int m, n, co;
char c[1000];
int all;
int dfs(int all) {
if(co == m) {
int a = 0, b = 0;
for(int i = 1; i <= all; i++) {
cout << c[i];
a++;
if(a == 4 && i != all) {
a = 0;
b++;
if(b == 16) {
cout << endl;
continue;
}
cout << " ";
}
}
cout << endl << all << endl;
return 1;
}
for(int i = 0; i < n; i++) {
c[all+1] = 'A' + i;
int p;
int q = 1;
for(int j = 1; j * 2 <= all + 1; j++) {
p = 0;
for(int k = 0; k < j; k++) {
if(c[all+1-k] != c[all+1-j-k]) {
p = 1;
break;
}
}
if(p == 0) {
q = 0;
break;
}
}
if(q) {
co++;
if(dfs(all+1) == 1)
return 1;
}
}
return 0;
}
int main() {
while(cin >> m >> n) {
if(m == 0 && n == 0)
break;
co = 0;
dfs(0);
}
return 0;
}