现在的位置: 首页 > 综合 > 正文

hdu2825—Wireless Password(AC自动机+状压dp)

2019年02月13日 ⁄ 综合 ⁄ 共 4000字 ⁄ 字号 评论关闭

Wireless Password
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4688 Accepted Submission(s): 1433

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 2 hello world 4 1 1 icpc 10 0 0 0 0 0

Sample Output

2 1 14195065

Source
2009 Multi-University Training Contest 1 - Host by TJU

Recommend
gaojie | We have carefully selected several similar problems for you: 2819 2824 2817 2823 2822

Statistic | Submit | Discuss | Note

dp[i][j][k]表示在节点k, 长度为i,状态为j时的方案数

/*************************************************************************
    > File Name: hdu2825.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年03月07日 星期六 10时40分25秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int MAX_NODE = 200;
const int CHILD_NUM = 26;
const int mod = 20090717;

int dp[30][(1 << 10) + 10][MAX_NODE];
char buf[30];
int num[1200];

struct AC_Automation
{
    int next[MAX_NODE][CHILD_NUM];
    int fail[MAX_NODE];
    int end[MAX_NODE];
    int root, L;

    int newnode ()
    {
        for (int i = 0; i < CHILD_NUM; ++i)
        {
            next[L][i] = -1;
        }
        end[L++] = 0;
        return L - 1;
    }

    void init ()
    {
        L = 0;
        root = newnode();
    }
    void Build_Trie (char buf[], int id)
    {
        int now = root;
        int len = strlen (buf);
        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_AC()
    {
        queue <int> qu;
        fail[root] = root;
        for (int i = 0; i < CHILD_NUM; ++i)
        {
            if (next[root][i] == -1)
            {
                next[root][i] = root;
            }
            else
            {
                fail[next[root][i]] = root;
                qu.push (next[root][i]);
            }
        }
        while (!qu.empty())
        {
            int now = qu.front();
            end[now] |= end[fail[now]];
            qu.pop();
            for (int i = 0; i < CHILD_NUM; ++i)
            {
                if (next[now][i] == -1)
                {
                    next[now][i] = next[fail[now]][i];
                }
                else
                {
                    fail[next[now][i]] = next[fail[now]][i];
                    qu.push(next[now][i]);
                }
            }
        }
    }

    void solve(int n, int m, int lim)
    {
        for (int i = 0; i <= n; ++i)
        {
            for (int k = 0; k < (1 << m); ++k)
            {
                for (int j = 0; j <= L; ++j)
                {
                    dp[i][k][j] = 0;
                }
            }
        }
        dp[0][0][0] = 1;
        for (int i = 0; i < n; ++i)
        {
            for (int k = 0; k < (1 << m); ++k)
            {
                for (int j = 0; j < L; ++j)
                {
                        if (!dp[i][k][j])
                        {
                            continue;
                        }
                        for (int l = 0; l < CHILD_NUM; ++l)
                        {
                            int now = next[j][l];
                            dp[i + 1][k | end[now]][now] += dp[i][k][j];
                            dp[i + 1][k | end[now]][now] %= mod;
                        }
                }
            }   
        }
        int ans = 0;
        for (int i = 0; i < L; ++i)
        {
            for (int j = 0; j < (1 << m); ++j)
            {
                if (num[j] >= lim)
                {
                    ans += dp[n][j][i];
                    ans %= mod;
                }
            }
        }
        printf("%d\n", ans);
    }
}AC;

int main ()
{
    int n, m, k;
    for (int i = 0; i <= 1024; ++i)
    {
        num[i] = 0;
        for (int j = 0; j <= 11; ++j)
        {
            if (i & (1 << j))
            {
                ++num[i];
            }
        }
    }
    while (~scanf("%d%d%d", &n, &m, &k) && (n + m + k))
    {
        AC.init();
        for (int i = 0; i < m; ++i)
        {
            scanf("%s", buf);
            AC.Build_Trie(buf, i);
        }
        AC.Build_AC();
        AC.solve(n, m, k);
    }
    return 0;
}

抱歉!评论已关闭.