Wireless Password

来源:互联网 发布:两个ip指向一个域名 编辑:程序博客网 时间:2024/05/21 09:07

题目描述

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. 

输入描述:

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.

输出描述:

For each test case, please output the number of possible passwords MOD 20090717.
示例1

输入

10 2 2hello world 4 1 1icpc 10 0 00 0 0

输出

2114195065
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <vector>using namespace std;const int mod = 20090717;const int maxn = 103;const int max_num = 26;int idx[256];int n, m, x;int dp[26][103][1026];bool vis[26][103][1026];int cnt[1025];struct node {    int v, p, zt;    node(){}    node(int v, int p, int zt) : v(v), p(p), zt(zt){}}Q[1000006];struct AcAuto {    int val[maxn], f[maxn];    int ch[maxn][max_num], tot;     void init() {        tot = 0;        new_node();        int i;        for(i = 0; i < 26; i++)            idx['a'+i] = i;    }    inline int new_node() {        memset(ch[tot], 0, sizeof(ch[tot]));        val[tot] = 0;        f[tot] = 0;        return tot++;    }     void insert(char *s, int id) {        int i, j, p = 0;        for(;*s; s++) {            int k = idx[*s];            if(!ch[p][k]) ch[p][k] = new_node();            p = ch[p][k];        }        val[p] |= 1<<id;    }    void getfail() {        int i, j, p = 0;        int q[maxn];        int *s = q, *e = q;        for(i = 0; i < max_num; i++) if(ch[0][i]) *e++ = ch[0][i];        while(s != e) {            int u = *s++;            for(i = 0; i < max_num; i++) {                int &v = ch[u][i];                if(!v) { v = ch[f[u]][i]; continue; }                *e++ = v;                j = f[u];                while(j && !ch[j][i]) j = f[j];                f[v] = ch[j][i];                val[v] |= val[f[v]];            }        }    }    void solve() {        int i, j, k, u;        int M = (1<<m);        for(i = 0; i <= n; i++)            for(k = 0; k < tot; k++)                for(j = 0; j < M; j++)                    dp[i][k][j] = 0;        dp[0][0][0] = 1;         node *s = Q, *e = Q;        *e++ = node(0, 0, 0);        vis[0][0][0] = 1;        while(s != e) {            node u = *s++;            vis[u.v][u.p][u.zt] = 0;            if(u.v >= n) continue;             for(i = 0; i < max_num; i++) {                int p = ch[u.p][i];                node v = node(u.v+1, p, u.zt|val[p]);                 dp[v.v][v.p][v.zt] += dp[u.v][u.p][u.zt];                if(dp[v.v][v.p][v.zt] >= mod) dp[v.v][v.p][v.zt] -= mod;                 if(!vis[v.v][v.p][v.zt]) {                    vis[v.v][v.p][v.zt] = 1;                    *e++ = v;                }            }        }         int ans = 0;        for(i = 0; i < M; i++) {            if(cnt[i] >= x)            for(j = 0; j < tot; j++) {                ans += dp[n][j][i];                if(ans >= mod) ans -= mod;            }        }        printf("%d\n", ans);    }}AC; char str[13];int main() {    int i, j;    for(i = 0; i < 1024; i++) {        int c = 0;        for(j = i; j; j -= (j&-j)) c++;        cnt[i] = c;    }    while( ~scanf("%d%d%d", &n, &m, &x) && (n || m || x)) {        AC.init();        for(i = 0; i < m; i++) {            scanf("%s", str);            AC.insert(str, i);        }        AC.getfail();        AC.solve();    }    return 0;}


原创粉丝点击