zoukankan      html  css  js  c++  java
  • HDU 2825 Wireless Password AC自动机+状压DP

    Wireless Password

    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
     
    题意:  
         有个人想破解他邻居的密码,他邻居告诉了一些关于这个密码的信息,并且给他一个单词集合,他用这些信息判断一下最少有多少种密码。
             1: 所有的密码都是有小写字母组成,长度固定n。
     
             2: 密码至少包含 k 种单词。
    题解:
         
          利用失配过程进行DP
        m 最大只有10,利用这点,我们可以状压
        设定DP[i][j][x] 表示在形成了长度为i的字符串时当前在trie树节点j上出现单词种类状态为x的方案数,
        暴力怼,最后统计答案
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    const int N=110,mod=20090717,inf=2e9+10;
    
    int q[N],head,tail,fail[N],nex[N][27],sum[N],n,m,k,cnt,dep[N];
    char a[N][40];
    void insert(char *s,int who) {
        int now = 1, len = strlen(s);
        for(int i = 0; i < len; ++i) {
            int index = s[i] - 'a';
            if(!nex[now][index]) nex[now][index] = ++cnt;
            dep[nex[now][index]] = dep[now] + 1;
            now = nex[now][index];
        }
        sum[now] |= (1<<(who-1));
    }
    
    void build_fail() {
        head = 0, tail = 0;
        for(int i = 0; i < 26; ++i) nex[0][i] = 1;
        fail[1] = 0;
        q[tail++] = 1;
        while(head != tail) {
            int now = q[head++];
            sum[now] |= sum[fail[now]];
            for(int i = 0; i < 26; ++i) {
                int p = fail[now];
                if(!nex[now][i]) {
                    nex[now][i] = nex[p][i];continue;
                }
                fail[nex[now][i]] = nex[p][i];
                q[tail++] = nex[now][i];
            }
        }
    }
    
    int dp[30][110][1029],num[1029];
    
    void solve() {
        for(int i = 0; i < n; ++i){
            for(int j = 0; j < tail; ++j) {
                for(int g = 0; g < (1<<m); ++g) {
                    if(dp[i][q[j]][g])
                    for(int index = 0; index < 26; ++index) {
                        int p = nex[q[j]][index];
                        dp[i+1][p][g|sum[p]]+=dp[i][q[j]][g]%mod;
                        dp[i+1][p][g|sum[p]]%=mod;
                    }
                }
            }
        }
    }
    
    int check(int j){
        int ss = 0;
        for(int i = 0; i < 30; ++i) {
            if(j&(1<<i)) ss++;
        }
        return ss;
    }
    
    int main() {
        for(int i = 0; i < 1025; ++i) {
            num[i] = check(i);
        }
        while(scanf("%d%d%d",&n,&m,&k)!=EOF) {
            if(n == 0 && m == 0 && k == 0) break;
            memset(nex,0,sizeof(nex));
            cnt = 1;
            memset(sum,0,sizeof(sum));
            for(int i = 1; i <= m; ++i) {
                scanf("%s",a[i]);
                insert(a[i],i);
            }
            build_fail();
            memset(dp,0,sizeof(dp));
            dp[0][1][0] = 1;
            solve();
            int ans = 0;
            for(int i = 0; i < tail; ++i)
            for(int j = 0; j < (1<<m); ++j) {
                if(num[j] < k) continue;
                ans = ( ans + dp[n][q[i]][j] ) % mod;
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    go语言笔记——append是内置的函数!!!new是一个函数!!!调试可以使用闭包,本质上是print调试,尼玛!
    go语言笔记——defer作用DB资源等free或实现调试
    提高mysql查询效率的六种方法
    无线网络wifi (WPA/WPA2)密码破解方法
    wifi密码破解方法总结(含破解软件下载链接)
    linux 创建账户
    vim中设置tab的长度的方法
    JAVAC 命令使用方法
    Linux环境下编译并执行ava helloworld程序
    java hadoop file system API
  • 原文地址:https://www.cnblogs.com/zxhl/p/6534784.html
Copyright © 2011-2022 走看看