0. The output of this problem is very cumbersome to handle.
Later similar can be used for reference
for(intI=0; i<cur;i++) { if(i!=0&& i% -==0) printf ("\n%c", A[i]); Else if(i!=0&& i%4==0) printf ("%c", A[i]); Elseprintf ("%c", A[i]); }
1. There is also a output nth small how to control nth small use is a initialized to 0 CNT whenever all conditions are satisfied to enter the recursive boundary to determine whether CNT equals N and then cnt++
The reason for the initialization to 0 is that the first time the call in the main function to execute the judgment statement cnt++ = = n when CNT is updated to 1, so it is initialized to 0 instead of 1.
2. Is the method of judging whether it is a simple string
This is a string consisting of the first L characters, so there is a i<l for loop and a[cur]= ' a ' +i;
int OK is 1 for difficult string, because after judgment is difficult string, only enter recursive DFS
After that, we are judging whether the suffix is the same, not the entire string to judge, that will do a lot of repetitive work resulting in efficiency degradation.
Initialize the isequal=1, then start judging, and once you find the unequal, isequal=0 then break
Outside of the loop to determine if (isequanl==1) if is 1 is a simple string is ok=0;
If ok==1 goes into recursion it's such a logical
for(intI=0; i<l;i++) {A[cur]='A'+i; intok=1; for(intj=1; j*2<=cur+1; j + +) { intIsequal=1; for(intk=0; k<j;k++) if(a[cur-k]!=a[cur-j-K]) {isequal=0; Break;} if(isequal==1) { //if it is a simple string then ok=0;ok=0; Break; } } if(ok==1&& getans==false) DFS (cur+1); }
3. After getting the answer remember to update Getans to True to terminate the remaining DFS otherwise it will always output
Or DFS joins the return value, recursive search process if there is a success, the direct exit!!! After you find the answer using the Int type DFS, return 0 The rest of Dfs return 1
if if (!dfs (cur+1return0;
1#include <cstdio>2 intn,l,cnt;3 Chara[ -];4 BOOLGetans;5 voidDfsintcur)6 {7 if(cnt++ = =N)8 {9getans=true;Ten for(intI=0; i<cur;i++) One { A if(i!=0&& i% -==0) -printf"\n%c", A[i]); - Else if(i!=0&& i%4==0) theprintf"%c", A[i]); - Else -printf"%c", A[i]); - } +printf"\ n"); - +printf"%d\n", cur); A } at for(intI=0; i<l;i++) - { -a[cur]='A'+i; - intok=1;//Ok=1 represents a difficult string - for(intj=1; j*2<=cur+1; j + +) - { in intIsequal=1; - to for(intk=0; k<j;k++) + if(a[cur-k]!=a[cur-j-K]) -{isequal=0; Break;} the * if(isequal==1) $ {Panax Notoginseng //if it is a simple string then ok=0; -ok=0; Break; the } + } A if(ok==1&& getans==false) DFS (cur+1); the } + } - intMain () $ { $ while(SCANF ("%d%d", &n,&l)! =EOF) - { - if(n==0&& l==0) Break; thegetans=false; -Cnt=0;WuyiDfs0); the } - Wu return 0; -}
Uva 129 Krypton Factor