Wireless Password

40 篇文章 0 订阅
28 篇文章 9 订阅

题目描述

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

输出

2
1
14195065
#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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值