zoukankan      html  css  js  c++  java
  • HDU2825 Wireless Password

    Description

    Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless network in the building. Liyuan did not know the password of the network, but he got some important information from his neighbor. He knew the password consists only of lowercase letters 'a'-'z', and he knew the length of the password. Furthermore, he got a magic word set, and his neighbor told him that the password included at least k words of the magic word set (the k words in the password possibly overlapping). 

    For instance, say that you know that the password is 3 characters long, and the magic word set includes 'she' and 'he'. Then the possible password is only 'she'. 

    Liyuan wants to know whether the information is enough to reduce the number of possible passwords. To answer this, please help him write a program that determines the number of possible passwords. 
     

    Input

    There will be several data sets. Each data set will begin with a line with three integers n m k. n is the length of the password (1<=n<=25), m is the number of the words in the magic word set(0<=m<=10), and the number k denotes that the password included at least k words of the magic set. This is followed by m lines, each containing a word of the magic set, each word consists of between 1 and 10 lowercase letters 'a'-'z'. End of input will be marked by a line with n=0 m=0 k=0, which should not be processed.
     

    Output

    For each test case, please output the number of possible passwords MOD 20090717.
     

    Sample Input

    10 2 2
    hello world
    4 1 1
    icpc
    10 0 0
    0 0 0
     

    Sample Output

    2 1 14195065
     
    dp
    #include<queue>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    const int LO=26,NU=101,MOD=20090717;
    inline int f(char u){
        return u-'a';
    }
    inline void M(int &a){
        if (a>=MOD) a-=MOD;
    }
    struct tree{
        int f;
        int q;
        int t[LO];
        int v[LO];
    }t[NU];
    int n,m,p,num;
    char s[1000];
    bool us[NU];
    queue <int> q;
    int dp[26][NU][1<<10];
    inline int dfs(int x){
        if (x==0) return 0;
        if (us[x]) return t[x].q;
        us[x]=1;
        return t[x].q|=dfs(t[x].f);
    }
    inline void in(int x){
        int p=0,l,m=strlen(s);
        for (register int i=0;i<m;i++){
            l=f(s[i]);
            if (!t[p].t[l]) t[p].t[l]=++num;
            p=t[p].t[l];
        }
        t[p].q=1<<x;
    }
    inline void mafa(){
        register int i;int k,p;
        q.push(0);t[0].f=0;
        while(!q.empty()){
            k=q.front();q.pop();
            for (i=0;i<LO;i++)
            if (t[k].t[i]){
                p=t[k].f;
                while((!t[p].t[i])&&p) p=t[p].f;
                t[t[k].t[i]].f=(k==p)?0:t[p].t[i];
                q.push(t[k].t[i]);
            }
        }
    }
    int main(){
        register int i,j,k,l;int u;int ans;
        while(scanf("%d%d%d",&n,&m,&p)){
            if (!(n|m|p)) break;
            num=u=ans=0;
            for (i=0;i<m;i++){
                   scanf("%s",s);
                in(i);
            }
            mafa();
               for (i=0;i<=num;i++) us[i]=0;
               for (i=0;i<=num;i++)
            t[i].q|=dfs(i);
            dp[0][0][0]=1;
            for (i=0;i<=num;i++)
            for (j=0;j<LO;j++){
                if (!t[i].t[j]){
                    u=t[i].f;
                    while(!t[u].t[j]&&u) u=t[u].f;
                    u=t[u].t[j];
                }else u=t[i].t[j];
                t[i].v[j]=u;
            }
            for (i=0;i<n;i++)
            for (j=0;j<=num;j++)
            for (k=0;k<(1<<m);k++)
            if (dp[i][j][k]!=0)
            for (l=0;l<LO;l++){
                u=k|t[t[j].v[l]].q;
                M(dp[i+1][t[j].v[l]][u]+=dp[i][j][k]);
            }
            for (i=0;i<=num;i++)
            for (k=0;k<(1<<m);k++){
                u=0;
                for (j=0;j<m;j++) if (k&(1<<j)) u++;
                if (u>=p) M(ans+=dp[n][i][k]);
            }
            printf("%d
    ",ans);
            for (i=0;i<=n;i++)
            for (j=0;j<=num;j++)
            for (k=0;k<(1<<m);k++) dp[i][j][k]=0;
            for (i=0;i<=num;i++)
            for (j=0;j<LO;j++) t[i].t[j]=t[i].v[j]=0;
            for (i=0;i<=num;i++) t[i].q=t[i].f=0;
        }
    }
    View Code
  • 相关阅读:
    DFS染色解决区域分块问题UVALive 6663
    栈之逆波兰
    线段树总结
    区间合并问题
    线段树的开闭区间问题
    离散化
    线段树的学习过程
    BFS的小结
    状态数组哪家强
    卡特兰数。
  • 原文地址:https://www.cnblogs.com/Enceladus/p/5308731.html
Copyright © 2011-2022 走看看