HDU2825 Wireless Password(状态压缩DP)

来源:互联网 发布:linux打开html文件 编辑:程序博客网 时间:2024/05/29 08:50

Wireless Password

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5817    Accepted Submission(s): 1857

Problem 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 2hello world 4 1 1icpc 10 0 00 0 0
 
Sample Output
2114195065
 
Source
2009 Multi-University Training Contest 1 - Host by TJU 
 
Recommend
goalie


比较综合的题目。AC自动机+DP
dp[i][j][k]表示长度为i的串匹配到状态j且字符串中的各个magic word的出现情况为k时的串的个数。i<=25,j<=100,k<=2^10-1。

const int MOD=20090717;int dp[30][110][1<<10];struct Trie{    int next[110][26],fail[110],end[110];    int root,L;    int newnode()    {        for(int i = 0;i < 26;i++)            next[L][i] = -1;        end[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++)        {            if(next[now][buf[i]-'a']==-1)                next[now][buf[i]-'a'] = newnode();            now = next[now][buf[i]-'a'];        }        end[now] |= (1<<id);    }    void build()    {        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();            end[now] |= end[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()    {        //memset(dp,0,sizeof(dp));        for(int i = 0;i <= n;i++)            for(int j = 0;j < L;j++)                for(int p = 0;p < (1<<m);p++)                    dp[i][j][p]=0;        dp[0][0][0] = 1;        for(int i = 0;i < n;i++)            for(int j = 0;j < L;j++)                for(int p = 0;p< (1<<m);p++)                    if(dp[i][j][p] > 0)                    {                        for(int x = 0;x < 26;x++)                        {                            int newi = i+1;                            int newj = next[j][x];                            int newp = (p|end[newj]);                            dp[newi][newj][newp] += dp[i][j][p];                            dp[newi][newj][newp]%=MOD;                        }                    }        int ans = 0;        for(int p = 0;p < (1<<m);p++)        {            if(num[p] < k)continue;            for(int i = 0;i < L;i++)            {                ans = (ans + dp[n][i][p])%MOD;            }        }        return ans;    }};



关于程序算法艺术与实践更多讨论与交流,敬请关注本博客和新浪微博songzi_tea.


0 0