题意:求最接近当前串的全排列顺序(字典顺序)的下一个串是否存在,若存在则输出下一个串, 若不存在 则输出"No Successor",不存在即是指当前串已经是全排列顺序的最后一个串了。例如abaacb 下一个全排列串为ababac 而cbbaa 不可能再找到比它大的全排列串了,所以不存在下一个串了。
题解:STL水过,利用next_permutation 默认就是生成字典顺序的全排列串来判断是否当前串是最后一个串。
代码:
/*
uva:ID Codes
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
char GStr[50+5]={'\0'};
char SortStr[50+5]={'\0'};
int GStrLen=0;
/*for test*/
int test()
{
return(0);
}
/*main process*/
int MainProc()
{
while(scanf("%s",GStr)!=EOF&&GStr[0]!='#')
{
GStrLen=strlen(GStr);
strcpy(SortStr,GStr);
sort(SortStr,SortStr+GStrLen);
next_permutation(GStr,GStr+GStrLen);
if(strcmp(GStr,SortStr)==0)
{
printf("No Successor\n");
}
else
{
printf("%s\n",GStr);
}
}
return(0);
}
int main()
{
MainProc();
return(0);
}
本文介绍了一个使用STL库中的next_permutation函数来解决寻找字符串全排列下一个字典序串的问题。通过示例代码展示了如何判断当前串是否为全排列的最后一个串,并输出下一个串或提示不存在。
1581

被折叠的 条评论
为什么被折叠?



