SSL2893 谷仓的安保


原题链接

外网进不去

题目大意

输入 n ( 3 ≤ n ≤ 15 ) n(3\le n\le 15) n(3n15)个字母,取出 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&&notyy>=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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值