Details of the various abuses!!
Actually, it's a simple, deep search.
As a binary tree to understand: Each node has two branches: into the stack and out of the stack.
Pruning operation: only if the top element of the stack and the target character at the current position are the same, the stack will not be out.
Dfs writes three parameters: Depth search depth, Npush stack count, npop number of stacks
Npush used to record the number of stacks: The main judge is the current pressure stack is reasonable, and the element to be pressed into the original string position
Npop used to record the number of stacks: Determine the location of the generated target string elements
When npush==npop== the target string, the description generates an executable sequence of actions
Note that the output operation string must be controlled with the depth parameter, because the maximum length of the string has changed over multiple inputs, and only using depth to determine which part of the string is currently generated.
Sticker Code!
# include<iostream># include<string># include<cstdio># include<cstring>using namespacestd;stringSource;stringTarget;Charsolution[ $];Chars[ $];inttop;voidDfsintDepthintNpush,intNpop) { if(Npush = = Target.length () && Npop = =target.length ()) { for(inti =0; I < depth; i++) {cout<< solution[i]<<" "; } cout<<Endl; return; } if(Npush <target.length ()) {S[top++] =Source[npush]; Solution[depth]='I'; DFS (Depth+1, Npush +1, Npop); Top--; } if(Top >0&& S[top-1] ==Target[npop]) {Solution[depth]='o'; Chartemp = s[top-1]; Top--; DFS (Depth+1, Npush, Npop +1); S[top++] =temp; } return;}intMain () { while(cin>>source>>target) {Top=1; cout<<"["<<Endl; if(source.length () = =target.length ()) DFS (0,0,0); cout<<"]"<<Endl; } return 0;}
Deep Search ——— ZOJ 1004:anagrams by Stack