ps
容朕吐槽一句:
每写一道题要是能查一遍生词,英语估计都不用愁了【没错说你呢poj
这是一道排列的题–隶属algorithm库里的 next_permutation。
引自cpluplus的example
// next_permutation example
#include <iostream> // std::cout
#include <algorithm> // std::next_permutation, std::sort
int main () {
int myints[] = {1,2,3};
std::sort (myints,myints+3);
std::cout << "The 3! possible permutations with 3 elements:\n";
do {
std::cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';
} while ( std::next_permutation(myints,myints+3) );
std::cout << "After loop: " << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';
return 0;
}
样例输出】
The 3! possible permutations with 3 elements:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
After loop: 1 2 3
按照STL文档的描述,next_permutation函数将按字母表顺序生成给定序列的下一个较大的排列,直到整个序列为降序为止。
另外还有一种排列是相反的:
prev_permutation函数与之相反,是生成给定序列的上一个较小的排列。二者原理相同,仅遍例顺序相反,
本题:
求该字符串的下一个排列,要是不存在的话,就输出No Successor
题很简单啦:一个应用。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
int main()
{
char input[51];
while(scanf("%s",&input)!=EOF)
{
if(strcmp(input,"#") == 0)
{
break;
}
int len=strlen(input);
if(next_permutation(input,input+len))
{
printf("%s\n",input);
}
else printf("No Successor\n");
}
return 0;
}