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
题目大概:
求字典序的全排列,但是要至少有1个元音字母和2和辅音字母。
思路:
dfs
代码:
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m;
char a[30];
int map[30];
int dfs(int k ,int t,int y,int f)
{
if(t==n&&y>=1&&f>=2)
{
for(int i=1;i<=m;i++)
{
if(map[i])cout<<a[i];
}
cout<<endl;
return 0;
}
for(int i=k;i<=m;i++)
{
if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u')
{
map[i]=1;
dfs(i+1,t+1,y+1,f);
map[i]=0;
}
else
{ map[i]=1;
dfs(i+1,t+1,y,f+1);
map[i]=0;
}
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=m;i++)
{
cin>>a[i];
}
sort(a+1,a+m+1);
dfs(1,0,0,0);
return 0;
}