next_permutation全排列函数是一个十分好用而且强大的函数。要想更好的了解这个函数可以看https://blog.csdn.net/howardemily/article/details/68064377
个人感觉写的特别好,里面有要注意的点。这里上几个例题吧:
number1:牛客网练习赛23D托米的咒语;
链接:https://www.nowcoder.com/acm/contest/156/D
来源:牛客网
托米没有完成上一个任务,准备施展黑魔法推倒 1317
黑魔法咒语被描述为一个 长为 n 的,仅包含小写英文字母 'a'...'i' 的字符串,在托米所在的星球,魔法造成的每次有效伤害都是来自他的一个子序列,对于每一个 'a'... 'i' 的排列(共 9! 种),若作为咒语的子序列出现, 就会造成 1 的伤害
而咒语的总伤害为所有 'a'... 'i' 的排列造成的伤害值之和,托米能打出多少点的伤害,是否能击败 1317 呢?
输入描述:
一行输入一个字符串 s
输出描述:
一行输出一个数,表示伤害值
示例1
输入
复制
aabcdefghi
输出
复制
1
备注:
|s| ≤ 3000
这就是一个全排列找子串的问题:先定义一个主字符串"abcdefghi”然后进行全排列对比找答案、
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const ll maxn=1e7+10;
const int Maxn=3000+10;
int main()
{
char a[10]="abcdefghi";
char s[Maxn];
gets(s);
int ans = 0;
int t;
int i;
do{
t=0,i=0;
for(;a[i]!=0&&s[t]!=0;){
if(a[i]==s[t])i++,t++;
else t++;
}
if(i==9){
ans++;
/* for(int i=0;i<9;i++){
printf("%c ",a[i]);
}
printf("\n");*/
}
}while(next_permutation(a,a+9));
printf("%d\n",ans);
return 0;
}
number2:poj1256
You are to write a program that has to generate all possible words from a given set of letters.
Example: Given the word "abc", your program should - by exploring all different combination of the three letters - output the words "abc", "acb", "bac", "bca", "cab" and "cba".
In the word taken from the input file, some letters may appear more than once. For a given word, your program should not produce the same word more than once, and the words should be output in alphabetically ascending order.
Input
The input consists of several words. The first line contains a number giving the number of words to follow. Each following line contains one word. A word consists of uppercase or lowercase letters from A to Z. Uppercase and lowercase letters are to be considered different. The length of each word is less than 13.
Output
For each word in the input, the output should contain all different words that can be generated with the letters of the given word. The words generated from the same input word should be output in alphabetically ascending order. An upper case letter goes before the corresponding lower case letter.
Sample Input
3 aAb abc acba
Sample Output
Aab Aba aAb abA bAa baA abc acb bac bca cab cba aabc aacb abac abca acab acba baac baca bcaa caab caba cbaa
Hint
An upper case letter goes before the corresponding lower case letter.
So the right order of letters is 'A'<'a'<'B'<'b'<...<'Z'<'z'.
这里的考点是要自定义CMP,这也体现了next_permutation的强大;
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const ll maxn=1e7+10;
const int Maxn=20000+10;
bool com(char a,char b){
if(tolower(a)!=tolower(b))return tolower(a)<tolower(b);
else return a<b;
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n;
char ch[14];
cin>>n;
while(n--){
cin>>ch;
sort(ch,ch+strlen(ch),com);
do{
cout<<ch<<endl;
}while(next_permutation(ch,ch+strlen(ch),com));
}
return 0;
}