Have to admire this eldest brother, algorithm study so fine, said is also very clear: http://blog.csdn.net/morewindows/article/details/7370155 algorithm is not posted, I read his description of his writing.
Remove duplicate non-recursive full permutations:
How do I calculate the next permutation of a string? To consider the "926520" this string, we look forward to the first pair of contiguous increment numbers, "20", "52" are non-incrementing, "26" is to meet the requirements, said the previous number 2 is the replacement number, the replacement number of the subscript is called the replacement point, and then find a larger than the number of replacement of the minimum number (this , 0, 2 are not, 5 can, 5 and 2 Exchange to get "956220", and then the replacement point after the string "6220" upside down to get "950226."
#include <string> #include <algorithm> #include <iostream>using namespace Std;class nrp{private: String __str; int __length, __NUM;PUBLIC:NRP () {cout<< "please input a string: \ n"; while (Cin >>__str) {__num = 0; __length = __str.length (); Sort (__str.begin (), __str.end ()); cout<< "start to output\n"; do {cout<<__str<<endl; __num++; } while (nextpermutation () = = true); cout<< "The total number of permutation is:" <<__num<<endl; cout<< "Please input a string: \ n"; }} ~NRP () {} bool Nextpermutation () {int i; for (i = __length-2; i>=0; i--) {if (__str[i] < __str[i+1]) {char c = ' N '; int k; for (int j = i+1; j< __length; j + +) { if (__str[j] > __str[i]) {if (c = = ' + ') { c = __str[j]; K = J; } else if (c >= __str[j])//When there are duplicate characters, you must also exchange {C = __str[j]; K = J; }}} swap (__str[i], __str[k]); Reverse (__str.begin () +i+1, __str.end ()); return true; }} return false; }};void Main () {NRP NRP;}
Remove duplicate recursive full permutations:
In this way, we also get the rule of removing duplicates in the whole arrangement-- the whole permutation of the weight is the number of each number from the first number to the non-recurring digital exchange after it. in programming, the description is that the number of the first and the number of the J is exchanged, the requirement [I,J] does not have the numbers equal to the number of J.
#include <iostream> #include <vector> #include <string>using namespace Std;class permutation{private : int n,sum; String Str;public:permutation () {cout<< "please input a string:" <<endl; while (cin>>str) {n = str.length (); sum = 0; Getperm (0); Printsum (); cout<< "Please input a string:" <<endl; }} ~permutation () {} bool Isswap (int nbegin, int nend) {for (int i = nbegin; i < nend; i++) {if (str[i] = = Str[nend]) {return false; }} return true; } void Getperm (int k) {if (k = = n) {sum++; cout<<str<<endl; } else {for (int i = k; i < n;i++)//If only 4 elements for the first time are swap (0,0) swap (0,1) swap (0,2) swap (0,3) {if (Isswap (k, i) = = True) { Swap (str[i],str[k]); Getperm (k+1); Swap (str[i],str[k]); }}}} void Printsum () {cout<<sum<<endl; }};void Main () {permutation perm;}
The pure STL removes the entire array of duplicates:
#include <iostream> #include <algorithm> #include <string>using namespace std;void main () { String str; Cin >> str; Sort (Str.begin (), Str.end ()); cout << str << endl; while (Next_permutation (Str.begin (), Str.end ())) { cout << str << endl; }}
Full-permutation collection