Poj 1146 ID Codes + Poj 1833 排列 (全排列库函数)

来源:互联网 发布:动画人物设计软件 编辑:程序博客网 时间:2024/05/16 23:55

很早就用过的东西,昨天遇到题的时候发现忘了函数名怎么拼了。。。。

next_permutation的函数声明:

#include <algorithm>
bool next_permutation( iterator start, iterator end );

The next_permutation() function attempts to transform the given range of elements [start,end) into the next lexicographically greater permutation of elements. If it succeeds, it returns true, otherwise, it returns false.

该函数用于生成给定序列的下一个全排列,成功返回true,会将生成的全排列存储在原数组中,失败,返回false,说明此时当前序列已经是字典序最大的排列了。

类似的还有:pre_permutation();用于生成上一个全排列。

非STL实现可以参见:http://blog.csdn.net/dreamvyps/article/details/6067345

Poj 1146 ID Codes

题意描述非常复杂,其实就是判断下一个全排列是否存在。

#include <cstring>#include <string>#include <algorithm>using namespace std;char str[55];int main (){int len;while (~scanf("%s", str),str[0]!='#'){len=strlen(str);if (next_permutation(str,str+len))printf("%s\n", str);else printf("No Successor\n");}return 0;}

#include <string>#include <cstdio>#include <iostream>#include <algorithm>using namespace std; int main (){string str;while (cin >> str){if (str[0]=='#')break;if (next_permutation(str.begin(), str.end()))cout<<str<<endl;elseprintf("No Successor\n");}    return 0;}

Poj 1833 排列

详见:http://blog.csdn.net/whyorwhnt/article/details/8652434

原创粉丝点击