原题链接
题目大意
输入
n
(
3
≤
n
≤
15
)
n(3\le n\le 15)
n(3≤n≤15)个字母,取出
m
m
m个,要求至少要有
1
1
1个元音和
2
2
2个辅音并且这些字母必须按字典序排列,求所有的可能性,按字典序输出,只输出前
25000
25000
25000种排列
S
a
m
p
l
e
\mathbf{Sample}
Sample
I
n
p
u
t
\mathbf{Input}
Input
4 6
a t c i s w
S a m p l e \mathbf{Sample} Sample O u t p u t \mathbf{Output} Output
acis
acit
aciw
acst
acsw
actw
aist
aisw
aitw
astw
cist
cisw
citw
istw
解题思路
简简单单的 d f s dfs dfs,只需要遍历所有的组合可能性,再特判一下有没有 1 1 1个元音和 2 2 2个辅音即可
上代码
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,tot;
char a[36],b[36];
bool vis[36];
bool aeiou(char c)
{
return c=='a'||c=='e'||c=='i'||c=='o'||c=='u' ;
}
void print()
{
for(int i=1; i<=m; i++) cout<<b[i];
cout<<endl;
}
void dfs(int k,int filled,bool cxyy,int notyy)
{
if(k==n+1)
{
if(cxyy&&filled==m&¬yy>=2)
{
tot++;
if(tot>25000) exit(0);
print();
}
return;
}
if(filled>m) return;
b[filled+1]=a[k];
if(aeiou(a[k])) dfs(k+1,filled+1,true,notyy);
else dfs(k+1,filled+1,cxyy,notyy+1);
b[filled+1]=' ';
dfs(k+1,filled,cxyy,notyy);
}
int main()
{
cin>>m>>n;
for(int i=1; i<=n; i++) cin>>a[i];
sort(a+1,a+n+1);
dfs(1,0,false,0);
return 0;
}
完美切题 ∼ \sim ∼