今天一口气做了关于全排列的4道题目,把这些放到这里,总结一下~
poj 1256 输出全排序,取出全排序中重复的
poj 1732 输出全排列
poj 1833 输出某个序列后的第几个全排序
poj 1146 输出某个序列后的全排列,无输出No Successor
poj 1731主要的代码如下
- Source Code
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #define N 201 using namespace std; char str[N]; int f(char x) { if(x>='a' && x<='z') return (x-'a'+1)*2; else return (x-'A')*2+1; } bool cmp(char x, char y) { return f(x)<f(y); } int main() { cin>>str; int n=strlen(str); sort(str,str+n,cmp); do { cout<<str<<endl; }while( next_permutation(str,str+strlen(str),cmp) ); return 0; }
主要就是使用next_permutation函数~可以轻松解决,算是4道水题,不过练手了~
全排列问题解法总结
本文总结了四道关于全排列问题的解题方法,包括输出全排列、去除重复排列等,并通过具体代码示例展示了如何利用next_permutation函数简化算法实现。
1200

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



