Arrangement of strings
Title Description: http://ac.jobdu.com/problem.php?pid=1369
Enter a string that prints out all the permutations of the characters in the string in the dictionary order. For example, enter the string ABC and print out all the strings abc,acb,bac,bca,cab and CBA that can be arranged by the character A,b,c.
In two steps:
The first step: to find all the characters that may appear in the first position, that is, the first character and all the words after the Nonalphanumeric exchange one at a time;
Second step: fixed the first position of the character, the following all the characters of the arrangement .
Termination condition: When a character that requires a fixed position is ' + ' , the description is already arranged at the end of the string .
#include <iostream>using namespace STD;voidSwapChar*C1,Char* C2) {Chartemp = *C1; *C1 = *C2; *C2 = temp;}voidPermutaion (Char* PStr,Char* Pbegin) {if(*pbegin = =' + ') {cout<< pStr << Endl;return; } for(Char* p = pbegin; *p! =' + '; p++) {swap (pbegin, p);//Swap pbegin position and P positionPermutaion (PSTR, pbegin+1);//fixed begin, arranged on pbegin+1Swap (Pbegin, p);//Exchange back}}voidPermutaion (Char* pStr) {if(pStr = = NULL)return; Permutaion (PSTR, pStr);}intMain () {CharS[] ="ABC"; Permutaion (s);}
Combination of strings
Enter a string that prints out all the permutations of the characters in the string in the dictionary order. For example, enter the string ABC, then print out all the strings that can be combined by the character a,b,c A, B, C, AB, AC, BC, ABC
To find a character combination of n characters with a length of M:
Divides n characters into two parts, the first character and all the remaining characters
- If the combination contains the first character, in the remaining n-1 characters, select m-1
- If the combination does not contain the first character, in the remaining n-1 characters, select m
#include <iostream>#include <vector>using namespace STD;voidPrintvector (Const vector<char>&V) { for( vector<char>:: Const_iterator iter = V.begin (); Iter! = V.end (); iter++)cout<< *iter;cout<< Endl;}//Find a combination of M characters from N charactersvoidCombination (Char* PStr,intM vector<char>&V) {if(M = =0) {Printvector (v);return; }if(*pstr = =' + '|| M <0)return;//combination contains this character, select M-1 in the remaining charactersV.push_back (*PSTR); Combination (pstr+1, M-1, v);//combination does not contain this character, select M in the remaining charactersV.pop_back (); Combination (pstr+1, M, v);}voidCombination (Char* PStr,intN) {if(pStr = = NULL)return; vector<char>V for(inti =1; I <= N; i++) combination (pStr, I, v);//Determine the length of the combination}intMain () {CharS[] ="ABC"; Combination (s),sizeof(s)/sizeof(s[0]));}
Other related issues
The question of the cube and the eight Queens:
Find all combinations of characters and all permutations http://www.wengweitao.com/qiu-zi-fu-de-suo-you-zu-he-he-suo-you-pai-lie.html
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
28-Full array and combination of strings