HDU 2825 Wireless Password(自動機+DP),hdu2825

來源:互聯網
上載者:User

HDU 2825 Wireless Password(自動機+DP),hdu2825
Problem DescriptionLiyuan 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.
 
InputThere 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. 
OutputFor each test case, please output the number of possible passwords MOD 20090717. 
Sample Input

10 2 2hello world 4 1 1icpc 10 0 00 0 0
 
Sample Output
2114195065
 自動機+dp:設dp[i][j]k]為長度為i,到達自動機j節點,字串包含情況為k的方法數。則dp[0][0][0]=1;
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<bitset>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;const int INF = 0x3f3f3f3f;const int MOD=20090717;int dp[30][110][1<<10];int n,m,k;int cal(int x){    int cnt=0;    for(int i=0;i<10;i++)        if(x&(1<<i)) cnt++;    return cnt;}struct AC{     int next[110][26];     int fail[110];     int ed[110];     int root,L;     int newnode()     {         for(int i=0;i<26;i++)            next[L][i]=-1;         ed[L++]=0;         return L-1;     }     void init()     {         L=0;         root=newnode();     }     void Insert(char buf[],int id)     {         int len=strlen(buf);         int now=root;         for(int i=0;i<len;i++)         {             int id=buf[i]-'a';             if(next[now][id]==-1)                next[now][id]=newnode();             now=next[now][id];         }         ed[now]|=(1<<id);     }     void Build_AC()     {         queue<int>q;         fail[root]=root;         for(int i=0;i<26;i++)         {             if(next[root][i]==-1)                next[root][i]=root;             else             {                fail[next[root][i]]=root;                q.push(next[root][i]);             }         }         while(!q.empty())         {             int now=q.front();             q.pop();             ed[now]|=ed[fail[now]];             for(int i=0;i<26;i++)             {                 if(next[now][i]==-1)                    next[now][i]=next[fail[now]][i];                 else                 {                    fail[next[now][i]]=next[fail[now]][i];                    q.push(next[now][i]);                 }             }         }     }     int solve()     {         CLEAR(dp,0);         dp[0][0][0]=1;         for(int i=0;i<n;i++)         {             for(int j=0;j<L;j++)             {                 for(int k=0;k<(1<<m);k++)                 {                     if(dp[i][j][k])                     {                         for(int x=0;x<26;x++)                         {                             int id=next[j][x];                             int st=k|ed[id];                             dp[i+1][id][st]+=dp[i][j][k];                             dp[i+1][id][st]%=MOD;                         }                     }                 }             }         }         int ans=0;         for(int i=0;i<(1<<m);i++)         {             if(cal(i)>=k)             {                 for(int j=0;j<L;j++)                    ans=(ans+dp[n][j][i])%MOD;             }         }         printf("%d\n",ans);     }};AC A;int main(){    char str[20];    while(~scanf("%d%d%d",&n,&m,&k))    {        if(n+m+k==0) break;        A.init();        for(int i=0;i<m;i++)        {            scanf("%s",str);            A.Insert(str,i);        }        A.Build_AC();        A.solve();    }    return 0;}/**/


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.