Wireless Password HDU2825

题目描述:

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 

题目分析:

大致题意是你有一个长度为n的字符串,并且有m个目标字符串,若在这个字符串中至少有k个目标字符串的字符串个数是多少个,并取模。
这是一道AC自动机和状态压缩dp结合的题目。
AC自动机我抄的kuangbin的代码。原理的话可以看这个,我也是看了很久AC自动机的部分,基础不牢,地动山摇啊。
http://blog.csdn.net/niushuai666/article/details/7002823

代码如下:

#include 
#include 
#include 
#include 

using namespace std;
const int MOD=20090717;

char c[20];
int n,m,k;
int dp[30][110][1<<10];//dp[i][j][k]表示长度为i的字符串 在自动机节点j下 目标字符串在压缩之后的十进制数为k时的状态总数
int num[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;//tire树长度
    }
    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<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 DP()
    {
        memset(dp,0,sizeof(dp));
        dp[0][0][0]=1;
        for(int i=0; i0)
                    {
                        for(int t=0; t<26; t++)
                        {
                            dp[i+1][next[j][t]][p|end[next[j][t]]]+=dp[i][j][p];//状态转移方程
                            dp[i+1][next[j][t]][p|end[next[j][t]]]%=MOD;
                        }
                    }
                }
            }
        }
        int ans=0;
        for(int p=0; p<(1<

体会心得:

这倒题看起来不难,但是我花了很长时间才理解状态压缩dp的原理,理解其状态转移方程,节省存储量,用二进制表示状态真的非常精彩。还有关于AC自动机的部分,也显现出我对字符串这块内容的不了解,我还需要努力再努力啊。

你可能感兴趣的:(动态规划)