HDU 2825 Wireless Password (AC自动机,DP)

http://acm.hdu.edu.cn/showproblem.php?pid=2825

Wireless Password
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4560    Accepted Submission(s): 1381

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

2009 Multi-University Training Contest 1 - Host
by TJU

题意:

给出m个模式串,要求构造一长度为n的文本串,至少包括k种模式串,求有多少种可能的模式串。

分析:

m个模式串构建AC自动机,然后要在这AC自动机中走n步,至少经过k个单词结点。因为m<=10,显然可以用状压表示已经有哪几个单词结点。用dp[i][j][k]表示走了i步到AC自动机中的第j个结点,单词状态为k,由计数原理可推出状态转移方程:dp[i][j][k]=sum(dp[i-1][last_j][last_k]),last_j表示可以抵达第j个结点的上一个结点,last_k表示上一步的状态;因为第i步只和第i-1步有关,所以可以用滚动数组优化空间。

/*
 *
 * Author : fcbruce <[email protected]>
 *
 * Time : Thu 20 Nov 2014 10:01:45 AM CST
 *
 */
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10

#ifdef _WIN32
  #define lld "%I64d"
#else
  #define lld "%lld"
#endif

#define maxm
#define maxn 255

using namespace std;

const int mod = 20090717;

int dp[2][maxn][1024];

inline int add(int a,int b)
{
  return (a+b)%mod;
}

int q[maxn];

const int maxsize = 26;
struct ACauto
{
  int ch[maxn][maxsize];
  int val[maxn],nex[maxn],last[maxn];
  int sz;

  ACauto()
  {
    memset(ch[0],0,sizeof ch[0]);
    val[0]=0;
    sz=1;
  }

  void clear()
  {
    memset(ch[0],0,sizeof ch[0]);
    val[0]=0;
    sz=1;
  }

  int idx(char c)
  {
    return c-'a';
  }

  void insert(const char *s,int v)
  {
    int u=0;
    for (int i=0;s[i]!='\0';i++)
    {
      int c=idx(s[i]);
      if (ch[u][c]==0)
      {
        memset(ch[sz],0,sizeof ch[sz]);
        val[sz]=0;
        ch[u][c]=sz++;
      }
      u=ch[u][c];
    }
    val[u]=v;
  }

  void get_fail()
  {
    int f=0,r=-1;
    nex[0]=0;
    for (int c=0;c<maxsize;c++)
    {
      int u=ch[0][c];
      if (u!=0)
      {
        nex[u]=0;
        q[++r]=u;
        last[u]=0;
      }
    }

    while (f<=r)
    {
      int x=q[f++];
      for (int c=0;c<maxsize;c++)
      {
        int u=ch[x][c];
        if (u==0)
        {
          ch[x][c]=ch[nex[x]][c];
          continue;
        }
        q[++r]=u;
        int v=nex[x];
        nex[u]=ch[v][c];
        val[u]|=val[nex[u]];
      }
    }
  }

  int calc(int x)
  {
    int cnt=0;
    for (int i=0;i<32;i++)
      if (x&(1<<i)) cnt++;
    return cnt;
  }

  int DP(int l,int m,int k)
  {
    memset(dp,0,sizeof dp);
    dp[0][0][0]=1;
    int x=1;
    for (int i=0;i<l;i++,x^=1)
    {
      memset(dp[x],0,sizeof dp[x]);
      for (int j=0;j<sz;j++)
      {
        for (int s=0;s<m;s++)
        {
          if (dp[x^1][j][s]==0) continue;
          for (int c=0;c<maxsize;c++)
          {
            int &cur=dp[x][ch[j][c]][s|val[ch[j][c]]];
            cur=add(cur,dp[x^1][j][s]);
          }
        }
      }
    }

    int total=0;

    for (int i=0;i<m;i++)
    {
      if (calc(i)<k) continue;
      for (int j=0;j<sz;j++)
        total=add(total,dp[x^1][j][i]);
    }

    return total;
  }
}acauto;

char str[16];

int main()
{
#ifdef FCBRUCE
  freopen("/home/fcbruce/code/t","r",stdin);
#endif // FCBRUCE

  int n,m,k;

  while (scanf("%d%d%d",&n,&m,&k),n||m||k)
  {
    acauto.clear();
    for (int i=0;i<m;i++)
    {
      scanf("%s",str);
      acauto.insert(str,1<<i);
    }

    acauto.get_fail();

    printf("%d\n",acauto.DP(n,1<<m,k));
  }

  return 0;
}
时间: 2024-11-03 02:38:03

HDU 2825 Wireless Password (AC自动机,DP)的相关文章

hdu 2825 Wireless Password(ac自动机&amp;dp)

Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4022    Accepted Submission(s): 1196 Problem Description Liyuan lives in a old apartment. One day, he suddenly found that there

HDU 2825 Wireless Password AC自动机+dp

训练赛第二场的I题,上完体育课回来就把这题过了,今天训练赛rank1了,还把大大队虐了,而且我还过了这道题 (虽然我也就过了这道题...),第一次在比赛中手写AC自动机还带dp的,心情大好. 给一个字符串集合,求包含该集合超过K个字符的,长度为L的字符串的个数. 显然是在AC自动机上跑dp,设dp[u][L][k]表示当前在结点u,还要走L步,当前状态为k的个数.一开始第三维表示的是包含k个字符串,但是题目要求不含重复的,那就只能状压了.转移为dp[u][L][k]+=dp[v][L-1][nk

hdu 2825 Wireless Password(AC自动机+状压DP)

题目链接:hdu 2825 Wireless Password 题目大意:N,M,K,M个字符串作为关键码集合,现在要求长度为N,包含K个以上的关键码的字符串有多少个. 解题思路:AC自动机+dp,滚动数组,因为关键码个数不会超过10个,所以我们用二进制数表示匹配的状态.dp[i][j][k] 表示到第i个位置,j节点,匹配k个字符串. #include <cstdio> #include <cstring> #include <queue> #include <

hdu 2825 Wireless Password(ac自己主动机&amp;amp;dp)

Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4022    Accepted Submission(s): 1196 Problem Description Liyuan lives in a old apartment. One day, he suddenly found that there

HDU 2825 Wireless Password (AC自动机 + 状态压缩DP)

题目链接:Wireless Password 解析:给 m 个单词构成的集合,统计所有长度为 n 的串中,包含至少 k 个单词的方案数. AC自动机 + 状态压缩DP. DP[i][j][k]:长度为i的字符串匹配到状态j且包含k个magic word的可能字符串个数. AC代码: #include <algorithm> #include <iostream> #include <cstdio> #include <queue> #include <

HDU 2825 Wireless Password(自动机+DP)

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 p

HDU 2457 DNA repair AC自动机 + dp

http://acm.hdu.edu.cn/showproblem.php?pid=2457 首先把病毒串保存一下,然后对于每一个trie上的节点,跑一发AC自动机,建立一个trie图. 建立的时候,对应做一些修改. 比如,现在建立成了这个样子. 如果he是一个病毒串,那么应该相对应的,把she那个he的位置,标志上,它也是病毒串,也就是不能转移到这一个状态. 这个可以在buildfail的时候对应修改. dp, 设dp[i][j],表示处理到字符串的第i个,走到了AC自动机的第j个节点,变成了

[AC自动机+状压dp] hdu 2825 Wireless Password

题意: 给n,m,k ,再给出m个单词 问长度为n的字符串,至少在m个单词中含有k个的组成方案有多少种. 思路: 由于m最大是10,所以可以采取状压的思想 首先建立trie图,在每个单词的结束节点标记一个mark=(1<<id),id为单词的编号 然后需要注意的,对于每个节点,应该顺着fail指针遍历一遍, 把所有的mark取一个并集. 因为就是如果单词出现包含的话,比如 she和he 我拿了she,其实等于两个都拿了. dp[i][j][k]  i步在节点j状态k的方案数 然后就是一个四重循

HDU 2296 Ring (AC自动机 + DP)

题目链接:Ring 解析:m个有价值的串,字符串s在字符串str中的价值 = s在str中出现的次数 × s的价值.问价值最大的长度为n的串是什么. 本题需要输出字典序最小的 在DP的时候开一个数组记录结果即可. AC代码: #include <algorithm> #include <iostream> #include <cstdio> #include <queue> #include <cstring> using namespace s