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

    题目链接:https://vjudge.net/problem/HDU-2825

    Wireless Password

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 7733    Accepted Submission(s): 2509


    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

    题意:

    给出m个单词,问长度为n且至少含有k个单词的字符串有多少个?

    题解:

    1.把这m个单词插入到AC自动机中。

    2.设dp[i][j][status]为:长度为i,到达j状态(AC自动机中的状态),且含有单词的信息为status(状态压缩)的字符串有多少个。

    3.模拟在AC自动机上的跳动,求出dp数组。

    代码如下:

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <vector>
      6 #include <cmath>
      7 #include <queue>
      8 #include <stack>
      9 #include <map>
     10 #include <string>
     11 #include <set>
     12 using namespace std;
     13 typedef long long LL;
     14 const double EPS = 1e-6;
     15 const int INF = 2e9;
     16 const LL LNF = 9e18;
     17 const int MOD = 20090717;
     18 const int MAXN = 100+10;
     19 
     20 int num[1<<10], dp[30][110][1<<10];
     21 
     22 struct Trie
     23 {
     24     const static int sz = 26, base = 'a';
     25     int next[MAXN][sz], fail[MAXN], end[MAXN];
     26     int root, L;
     27     int newnode()
     28     {
     29         for(int i = 0; i<sz; i++)
     30             next[L][i] = -1;
     31         end[L++] = 0;
     32         return L-1;
     33     }
     34     void init()
     35     {
     36         L = 0;
     37         root = newnode();
     38     }
     39     void insert(char buf[], int id)
     40     {
     41         int len = strlen(buf);
     42         int now = root;
     43         for(int i = 0; i<len; i++)
     44         {
     45             if(next[now][buf[i]-base] == -1) next[now][buf[i]-base] = newnode();
     46             now = next[now][buf[i]-base];
     47         }
     48         end[now] |= (1<<id);
     49     }
     50     void build()
     51     {
     52         queue<int>Q;
     53         fail[root] = root;
     54         for(int i = 0; i<sz; i++)
     55         {
     56             if(next[root][i] == -1) next[root][i] = root;
     57             else fail[next[root][i]] = root, Q.push(next[root][i]);
     58         }
     59         while(!Q.empty())
     60         {
     61             int now = Q.front();
     62             Q.pop();
     63             end[now] |= end[fail[now]]; //当前串的后缀是否也包含单词
     64             for(int i = 0; i<sz; i++)
     65             {
     66                 if(next[now][i] == -1) next[now][i] = next[fail[now]][i];
     67                 else fail[next[now][i]] = next[fail[now]][i], Q.push(next[now][i]);
     68             }
     69         }
     70     }
     71 
     72     int query(int n, int m, int k)
     73     {
     74         for(int i = 0; i<=n; i++)
     75             for(int j = 0; j<L; j++)
     76                 for(int s = 0; s<(1<<m); s++)
     77                     dp[i][j][s] = 0;
     78         dp[0][0][0] = 1;
     79         for(int i = 0; i<n; i++)    //模拟在AC自动机上的跳动
     80             for(int j = 0; j<L; j++)
     81                 for(int s = 0; s<(1<<m); s++)
     82                 {
     83                     if(dp[i][j][s]==0) continue;
     84                     for(int p = 0; p<sz; p++)
     85                     {
     86                         int newi = i+1;
     87                         int newj = next[j][p];
     88                         int news = (s|end[newj]);
     89                         dp[newi][newj][news] += dp[i][j][s];
     90                         dp[newi][newj][news] %= MOD;
     91                     }
     92                 }
     93         int ret = 0;
     94         for(int s = 0; s<(1<<m); s++)
     95         {
     96             if(num[s]<k) continue;
     97             for(int i = 0; i<L; i++)
     98                 ret = (ret+dp[n][i][s])%MOD;
     99         }
    100         return ret;
    101     }
    102 };
    103 
    104 Trie ac;
    105 char buf[20];
    106 int main()
    107 {
    108     for(int s = 0; s<(1<<10); s++)
    109     {
    110         num[s] = 0;
    111         for(int i = 0; i<10; i++)
    112             if(s&(1<<i)) num[s]++;
    113     }
    114 
    115     int n, m, k;
    116     while(scanf("%d%d%d", &n,&m,&k)&&(n||m||k))
    117     {
    118         ac.init();
    119         for(int i = 0; i<m; i++)
    120         {
    121             scanf("%s", buf);
    122             ac.insert(buf, i);
    123         }
    124         ac.build();
    125         int ans = ac.query(n,m,k);
    126         printf("%d
    ", ans);
    127     }
    128     return 0;
    129 }
    View Code
  • 相关阅读:
    Clean Docker <none>:<none>
    Conservation Vs Non-conservation Forms of conservation Equations
    What are the differences between an LES-SGS model and a RANS based turbulence model?
    How to permanently set $PATH on Linux/Unix?
    tar解压命令
    C++ (P199—P211)多态 虚函数 抽象类
    C++ (P160—)多继承 二义性 虚基类 “向上转型”
    java与c++的访问权限的问题
    由strupr,strlwr体会如果将字符常量转换为变量进行修改,体会常量的静态存储
    C++ (P103—P154)
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/8455914.html
Copyright © 2011-2022 走看看