Several common uses:
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace Std;
int main ()
{
Char d[100];
cin>>d;
Sort (D,d+strlen (d));
Char *first=d;
Char *last=d+strlen (d);
do{
cout<<d<<endl;
}while (Next_permutation (first,last));
return 0;
}
////////////////////////////////////////////
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace Std;
int main ()
{
Char d[100];
cin>>d;
Sort (D,d+strlen (d));
Char *first=d;
Char *last=d+strlen (d);
do{
cout<<d<<endl;
}while (Next_permutation (first,last));
return 0;
}
//////////////////////////////////////////
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace Std;
int main ()
{
String Dog;
while (cin>>dog&&dog!= "@")
{
Sort (Dog.begin (), Dog.end ());
while (Next_permutation (Dog.begin (), Dog.end ()))
{
cout<<dog<<endl;
}
}
}
///////////////////////////////////////
A case of failure: (POJ 1009)
Title Description
Xiao Xiang classmate's dorm WiFi added a password, the password will change every day. And Xiao Xiang every day to the students to rub the net to provide password hints. Now please follow the password prompts, write the program to decipher the password. Known password hints give n integers a1,a2,..., An, and an integer t (t less than N). The addition of any of the n integers allows you to get a series of sum. For example, when n=4,t=3,4 integers are 3,7,12,19, all combinations and respectively are:
3+7+12=22 3+7+19=29 7+12+19=38 3+12+19=34. And the number of primes is the small Cheung dormitory wifi password.
For example, in the above example, there is only one and a prime number: 3+7+19=29, so the password is 1.
Input
N, t (1<=n<=20,t
Sample input4 3 3 7Sample output1
#include <iostream>#include<string.h>#include<algorithm>#include<stdio.h>#include<math.h>using namespacestd;Longd[ -],*head;inti,n,t,sum=0, mark=0, Q;intFunintNS) { intj,m; M=(int) sqrt (NS); for(j=2; j<=m;j++) { if(! (ns%j)) {return 0;} } return 1;}intpermutation () {if(q<t) {cout<<n<<q<<t<<"$";returnMark;} Do { for(i=0; i<t;i++) {sum=sum+D[i]; cout<<d[i]<<"*"; } if(sum) = =1) {Mark++; } Sum=0; } while(Next_permutation (d,d+Q)); Q--;cout<<"!"; Permutation ();}intMain () {Freopen ("Stdin.txt","R", stdin); while(SCANF ("%d%d", &n,&t)! =EOF) { for(i=0; i<n;i++) {scanf ("%ld",&D[i]); } Q=N; cout<<permutation () <<Endl; Mark=n=t=0; } return 0;//simultaneous merger, lack of situation}
View Code
Next_permutation encounter the same time the same sequence will be merged, use this problem will be wrong, pay attention to the conditions, the problem with DFS can.
Next_permutation usage and an unsuccessful case ~ (>_<)