1. Title Description: Click to open the link
2. Solving ideas: The problem is solved by backtracking. According to test instructions description, it is known that when enumerating the cur bits, it is only necessary to check that the string is valid after it is added, without having to check whether the string between cur is legal, because this step is already done before enumerating the cur. In addition, the output of the subject to compare pits, need to be careful.
3. Code:
#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace std; #define N 80+10int s[n];int N, l;int cnt; int dfs (int cur) {if (cnt++ = = N) {for (int i = 0; i < cur; i++) {printf ("%c", ' A ' + s[i]); if ((i + 1)% 4 = = 0) {if ((i + 1)% = = 0) cout << endl;else if (i! = cur-1) Putchar (');}} if (cur) cout << endl;printf ("%d\n", cur); return 0;//returns 0 indicating that an understanding of}for (int i = 0; i < L; i++) {s[cur] = i;int o K = 1;for (int j = 1; J * 2 <= cur + 1; + j)//enumeration suffix Length (note not the length of the entire string) {int equal = 1;for (int k = 0; k < j;k++)//check prefix and suffix is No equal, s[cur-k] is the suffix part, s[cur-k-j] is the prefix part if (s[cur-k]! = s[cur-k-j]) {equal = 0; break;} if (equal) {ok = 0; break;}} If(OK) if (!dfs (cur + 1)) return 0;//If the solution is found, exit}return 1 directly;} int main () {//freopen ("T.txt", "R", stdin), while (~SCANF ("%d%d", &n, &l) && (n| | L)) {cnt = 0;memset (s, 0, sizeof (s));d FS (0);} return 0;}
Example 7-5 difficult string UVa129