HDU-2825 wireless password (AC automation + dp)

Source: Internet
Author: User

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 has ded 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 has des '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 has ded 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 shoshould 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 

Question: let you construct a string with a length of N, so that at least K of the set containing a total of M can be used to calculate the number.
Idea: the idea of DP is required for the construction of AC automatic machines, if DP [I] [J] [k] is set, it indicates the number when the length of I reaches the status of J and the status of K. We need to use one to represent the number of strings contained in status J, that is, the number to the end of the string, which is expressed in binary. Note that the Fail pointer is transferred when constructing an automatic machine. Therefore, status J also needs to be combined with the State when it is not matched.

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int mod = 20090717;int n, m, k;int dp[30][250][1<<10];int num[5000];struct Trie {    int nxt[110][26], fail[110], end[110];    int root, sz;    int newNode() {        for (int i = 0; i < 26; i++)            nxt[sz][i] = -1;        end[sz++] = 0;        return sz - 1;    }    void init() {        sz = 0;        root = newNode();    }    void insert(char buf[], int id) {        int now = root;        for (int i = 0; buf[i]; i++) {            if (nxt[now][buf[i]-‘a‘] == -1)                nxt[now][buf[i]-‘a‘] = newNode();            now = nxt[now][buf[i]-‘a‘];        }         end[now] |= (1<<id);    }    void build() {        queue<int> q;        fail[root] = root;        for (int i = 0; i < 26; i++) {            if (nxt[root][i] == -1)                 nxt[root][i] = root;            else {                fail[nxt[root][i]] = root;                q.push(nxt[root][i]);            }        }        while (!q.empty()) {            int now = q.front();            q.pop();            end[now] |= end[fail[now]];            for (int i = 0; i < 26; i++) {                if (nxt[now][i] == -1)                    nxt[now][i] = nxt[fail[now]][i];                else {                    fail[nxt[now][i]] = nxt[fail[now]][i];                    q.push(nxt[now][i]);                }            }        }    }    int solve() {        for (int i = 0; i <= n; i++)            for (int j = 0; j < sz; j++)                for (int k = 0; k < (1<<m); k++)                    dp[i][j][k] = 0;        dp[0][0][0] = 1;        for (int i = 0; i < n; i++)            for (int j = 0; j < sz; j++)                for (int k = 0; k < (1<<m); k++)                     if (dp[i][j][k] > 0) {                        for (int x = 0; x < 26; x++) {                            int ni = i+1;                            int nj = nxt[j][x];                            int nk = k | end[nj];                            dp[ni][nj][nk] += dp[i][j][k];                            dp[ni][nj][nk] %= mod;                        }                    }        int ans = 0;        for (int p = 0; p < (1<<m); p++) {            if (num[p] < k) continue;            for (int i = 0; i < sz; i++)                ans = (ans + dp[n][i][p]) % mod;        }        return ans;    }} ac;char buf[20];int main() {    for (int i = 0; i < (1<<10); i++) {        num[i] = 0;        for (int j = 0; j < 10; j++)            if (i & (1<<j))                num[i]++;    }    while (scanf("%d%d%d", &n, &m, &k) != EOF && n+m+k) {        ac.init();        for (int i = 0; i < m; i++) {            scanf("%s", buf);            ac.insert(buf, i);        }        ac.build();        printf("%d\n", ac.solve());    }    return 0;}


HDU-2825 wireless password (AC automation + dp)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.