poj 3049 Securing the Barn 搜索

题意:给你C个字母,输出由L个字母组成并且包含至少一个元音字母和两个辅音字母的所有由L个字母组成的排列。

分析:dfs

View Code
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
char pan[30] , ss[5];
int L , C;
vector<char> V;
bool cmp(char a , char b) { return a < b; }
bool panpan(int u) {
    char cc = pan[u];
    if(cc == 'a' || cc == 'e' || cc == 'i' || cc == 'o' || cc == 'u') return true;
    return false;
}
void dfs(int u , int len , int a , int b) {
    if(len == L) {
        if(a < 1 || b < 2) return;
        for(int i=0;i<L;i++) putchar(V[i]);
        printf("\n");
        return;
    }
    if(u >= C) return;
    V.push_back(pan[u]);
    if(panpan(u)) dfs(u+1 , len + 1 , a + 1 , b);
    else dfs(u+1 , len+1 , a , b + 1);
    V.pop_back();
    dfs(u+1 , len , a , b);
}
int main() {
    while(~scanf("%d%d",&L,&C)) {
        for(int i=0;i<C;i++) {
            scanf("%s",ss);
            pan[i] = ss[0];
            pan[C] = '\0';
        }
        sort(pan , pan + C);
        dfs(0 , 0 , 0 , 0);
    }
    return 0;
}
posted @ 2012-07-05 19:24  lenohoo  阅读(296)  评论(0编辑  收藏  举报