本文主要是介绍排列组合问题系列,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
<p>1、不重复排列问题:</p><p>题目意思:给你一个只含小写英文字母的串,长度规模<=200。让输出所有不重复的排列。按照字典序从小到达。</p><p>想法:正在验证板子,就拿整数的不重复排列写了下,发现那个数据规模极小,哪怕是10个数,也会达到10!的规模。现在是200的长度,怎么能不run error,或者是我程序写挫了。另外,看到网上有人这么写,方法很巧,我们都知道递归写排列,那么怎么样才能导致不重复:将串先排序,在某一层次递归时,保证不和本层次递归上一轮的字符相同就ok。</p><p>2、全排列问题:</p><p>类循环和递归。</p>
int a[10]={0,1,2,3,4,5,6,7,8,9};
int main(){#ifdef ACBang
// freopen("in.txt","r",stdin);#endifint CAS,n,m;sf("%d",&CAS);while(CAS--){sf("%d%d",&n,&m);int pre=0;do{if(a[m]==pre)continue;REP1(i,m)pf("%d",a[i]);pf("\n");pre=a[m];}while(next_permutation(a+1,a+n+1));}rt 0;
}
int n,m;
int a[10]={0,1,2,3,4,5,6,7,8,9};
int b[10];
int mark[10];
void dfs(int b[],int len){if(len==m){REP0(i,len)pf("%d",b[i]);pf("\n");rt ;}REP1(i,n){if(mark[a[i]]==1)continue;b[len]=a[i];mark[a[i]] = 1;dfs(b,len+1);mark[a[i]] = 0;}
}
int main(){#ifdef ACBang
// freopen("in.txt","r",stdin);#endifint CAS;sf("%d",&CAS);while(CAS--){sf("%d%d",&n,&m);ms(mark,0,sizeof(mark));dfs(b,0);}rt 0;
}
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#define rt return
#define bk break
#define ct continue
#define sf scanf
#define pf printf
#define ms memset
#define si(n) sf("%d",&n)
#define pi(n) pf("%d",n)
#define REP0(i,n) for(int i=0;i<(n);i++)
#define REP1(i,n) for(int i=1;i<=(n);i++)
#define REP(i,s,n) for(int i=s;i<=(n);i++)
#define db double
#define pb push_back
#define LL long long
#define INF 0x3fffffff
#define eps 1e-8
#define PI acos(-1)
#define maxn 2200
using namespace std;
int n,m,cnt;
int rcd[maxn];
int used[maxn];
int num[maxn];
char str[maxn];
struct node{char s[maxn];
}ans[maxn];
void unrepeat_permutation(int l){int i;if(l==n){REP0(i,n) ans[cnt].s[i]=rcd[i]+'a';cnt++;rt ;}REP0(i,m){if(used[i]>0){used[i]--;rcd[l]=num[i];unrepeat_permutation(l+1);used[i]++;}}
}
int read_data(){int i,j,val;m=0;n = strlen(str);REP0(i,n){val = str[i]-'a';for(j=0;j<m;j++){if(num[j]==val){used[j]++;break;}}if(j==m){num[m]=val;used[m++]=1;}}rt 1;
}
bool cmp(node a,node b){return strcmp(a.s,b.s)<0;
}
int main(){#ifdef ACBang
// freopen("in.txt","r",stdin);#endifwhile(~sf("%s",str)){cnt=0;read_data();unrepeat_permutation(0);sort(ans,ans+cnt,cmp);REP0(i,cnt)pf("%s\n",ans[i].s);}rt 0;
}
<pre name="code" class="cpp">#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#define rt return
#define bk break
#define ct continue
#define sf scanf
#define pf printf
#define ms memset
#define si(n) sf("%d",&n)
#define pi(n) pf("%d",n)
#define REP0(i,n) for(int i=0;i<(n);i++)
#define REP1(i,n) for(int i=1;i<=(n);i++)
#define REP(i,s,n) for(int i=s;i<=(n);i++)
#define db double
#define pb push_back
#define LL long long
#define INF 0x3fffffff
#define eps 1e-8
#define PI acos(-1)
#define maxn 220
using namespace std;
char str[maxn],s[maxn];
int mark[maxn];
void dfs(int len){if(len==strlen(str)){s[len]='\0';pf("%s\n",s);rt ;}char c='\0';REP0(i,strlen(str)){if(mark[i]==0&&str[i]!=c){mark[i]=1;s[len]=str[i];c=str[i];/!!!!!!!!!!!!!dfs(len+1);mark[i]=0;}}
}
int main(){#ifdef ACBang
// freopen("in.txt","r",stdin);#endifwhile(~sf("%s",str)){sort(str,str+strlen(str));dfs(0);}rt 0;
}
这篇关于排列组合问题系列的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!