UVA 146 ID编码

来源:互联网 发布:vb.net 使用资源文件 编辑:程序博客网 时间:2024/06/05 04:33

两种方式

1.STL  next_permutation求下一个排列

2.手动从最后一个字符向后搜索,找到比它小的交换,然后再对后面的序列进行排序

#include<iostream>#include<cstdio>#include<cstring>#include<memory>#include<queue>#include<algorithm>#include<string>#include<cmath>#include<stack>using namespace std;///char str[60];bool cmp(char a, char b){return a < b;}int main(){///int i, j, k;while (cin >> str){if (str[0] == '#')break;bool flag = false;bool is_find = false;int len = strlen(str);int pos1, pos2;for (i = len - 1; i >= 0; i--){for (j = i; j >= 0; j--){if (str[j] < str[i]){is_find = true;char temp = str[j];pos1 = j;pos2 = i;str[j] = str[i];str[i] = temp;break;}}if (is_find)break;}if (i < 0){cout << "No Successor" << endl;continue;}sort(str + pos1 + 1 , str + len, cmp);cout << str << endl;}    return 0;}