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.
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.
* 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
* Line 2: C space-separated lower-case characters that are the set of characters from which to build the passwords
* Lines 1..?: Each output line contains a word of length L characters (and no spaces). The output lines must appear in alphabetical order.
4 6 a t c i s w
acis acit aciw acst acsw actw aist aisw aitw astw cist cisw citw istw
INPUT DETAILS:
Passwords of length 4 chosen from the given six characters
Passwords of length 4 chosen from the given six characters
看上去挺麻烦的题,又要有元音又要有辅音要求,要分开?没必要,直接上,搜就可以的。搜到结果是加一个判断,看元音、辅音个数是否符合标准,可以就输出。开始没发现有顺序的,输入完直接sort()一下就行,char类型 cmp不用另写。
代码如下:
#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
int n,m;
map<char,int>mp;
char p[16];
char ans[16];
int vis[16];
int dfs(int t,char ch)
{
if(t==m)
{
int s=0;
for(int i=1;i<=t;i++)
{
if(mp[ans[i]]==1)
s++;
}
if(s>=1 && s<=(m-2))
{
for(int i=1;i<=t;i++)
cout<<ans[i];
cout<<endl;
}
}
for(int i=1;i<=n;i++)
{
if(vis[i]==0 && p[i]>ch)
{
vis[i]=1;
ans[t+1]=p[i];
dfs(t+1,p[i]);
vis[i]=0;
}
}
}
int main()
{
mp['a']=1;
mp['e']=1;
mp['i']=1;
mp['o']=1;
mp['u']=1;
while(cin>>m>>n && (m+n))
{
for(int i=1;i<=n;i++)
{
cin>>p[i];
}
sort(p+1,p+n+1);
for(int i=1;i<=n;i++)
{
vis[i]=1;
ans[1]=p[i];
dfs(1,p[i]);
vis[i]=0;
}
}
return 0;
}