Securing the Barn(dfs)

Securing the Barn
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2304 Accepted: 1466

Description

Farmer John has installed a new security system on the barn and now must issue a valid password to the cows in the herd. A valid password consists of L (3 <= L <= 15) different lower-case characters (from the traditional latin character set 'a'...'z'), has at least one vowel ('a', 'e', 'i', 'o', or 'u'), at least two consonants (non-vowels), and has characters that appear in alphabetical order (i.e., 'abc' is valid; 'bac' is not).

Given a desired length L along with C lower-case characters, write a program to print all the valid passwords of length L that can be formed from those letters. The passwords must be printed in alphabetical order, one per line.

Input

* Line 1: Two space-separated integers, L and C

* Line 2: C space-separated lower-case characters that are the set of characters from which to build the passwords

Output

* Lines 1..?: Each output line contains a word of length L characters (and no spaces). The output lines must appear in alphabetical order.

Sample Input

4 6
a t c i s w

Sample Output

acis
acit
aciw
acst
acsw
actw
aist
aisw
aitw
astw
cist
cisw
citw
istw

Hint

INPUT DETAILS:
Passwords of length 4 chosen from the given six characters

Source


题意:

从六个里选四个字母 需至少1个元音字母,两个辅音字母  然后进行全排列

思路:

搜索,首先对所有字母进行一个排序,然后进行搜索,以每个点作为起点开始搜。

代码:

#include <iostream>
#include <cstring>
#include <string>
#include <stdio.h>
#include <algorithm>
using namespace std;
int ans,sum;
char s[27];
int vis[27];//代表某个字母是否访问过
int l,c;
void dfs(int num,int len,int yuan,int fu)
{
    if(len==l&&yuan>=1&&fu>=2)
    {
        for(int i=0;i<c;i++)
        {
            if(vis[i])cout<<s[i];
        }
        cout<<endl;
return; 
}
    for(int i=num;i<c;i++)  //分情况,是元音还是辅音
    {
         if (s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u')
            {
                 vis[i]=1;
                 dfs(i+1,len+1,yuan+1,fu);
                vis[i]=0;
            }
        else
            {
                vis[i]=1;
                dfs(i+1,len+1,yuan,fu+1);
                vis[i]=0;
            }
    }
}
int main()
{

    int i;
    cin>>l>>c;
    for(i=0;i<c;i++)
    {
        cin>>s[i];
        vis[i]=0;
    }
    sort(s,s+c);  //排序
    dfs(0,0,0,0);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值