https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=65
题意:输出不包含两个相邻的重复子串。
思路:这就跟八皇后问题是一样的,注意判断子串是否相同就可以了。
1 #include<iostream> 2 using namespace std; 3 4 int n, l; 5 int str[100]; 6 int flag; 7 int cnt; 8 9 void dfs(int cur) 10 { 11 if (cnt++ == n) 12 { 13 for (int i = 0; i < cur; i++) 14 { 15 if (i % 64 == 0 && i) cout << endl; 16 else if (i % 4 == 0 && i) cout << " "; 17 char c = 'A' + str[i]; 18 cout << c; 19 } 20 flag = 1; 21 cout << endl; 22 cout << cur << endl; 23 return; 24 } 25 else 26 { 27 for (int i = 0; i < l; i++) 28 { 29 str[cur] = i; 30 int ok = 1; 31 for (int j = 1; 2 * j <= cur + 1; j++) 32 { 33 int equal = 1; 34 for (int k = 0; k < j; k++) 35 { 36 if (str[cur - k] != str[cur - k - j]) 37 { 38 equal = 0; 39 break; 40 } 41 } 42 if (equal) 43 { 44 ok = 0; 45 break; 46 } 47 } 48 if (ok) if(!flag) dfs(cur + 1); 49 } 50 } 51 } 52 53 int main() 54 { 55 while (cin >> n >> l && n) 56 { 57 flag = 0; 58 cnt = 0; 59 dfs(0); 60 } 61 return 0; 62 }