Problem one: for string s, the output string s all permutations of characters. For example: The input string "ABC", its full array is ABC,ACB,BAC,BCA,CAB,CBA;
Method One: This is a depth-first search process.
voidDFS (vector<string> &result,string& Path,stringSintLen) { if(len==s.size ()) {result.push_back (path); return; } for(intI=0; I<s.size (); i++) { if(Path.find (s[i]) ==-1) {path.push_back (s[i]); DFS (Result,path,s,len+1); Path.pop_back (); } }}intMain () {stringStr="ABC"; Vector<string>result; stringpath; DFS (RESULT,PATH,STR,0);}
Method Two: Recursive process: First of all may appear in the first position of the character, that is, the first character and all the characters after the exchange. Second, the first character is fixed, which is the full arrangement of the following characters, that is, exchanging the second character with all subsequent characters. Keep going.
voidPermutation (Char*s,Char*Sbegin) { if(*sbegin==' /') cout<<s<<Endl; Else { for(Char*p=sbegin;*p!=' /';p + +) {Swap (*sbegin,*p); Permutation (S,sbegin+1); Swap (*sbegin,*p); } }}intMain () {Chars[]="ABC"; Permutation (s,s);}
Word embox arrangement