pid=2825">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
 

题意:

给出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 <fcbruce8964@gmail.com>
*
* 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;
}

最新文章

  1. CentOS(RedHat)安装Adobe Flash Player插件 For firefox
  2. pxe无人值守安装多网卡注意事项
  3. 黑客入门之IP地址及常用命令
  4. Install Hive
  5. iOS之用xib给控件设置圆角、边框效果
  6. Eclipse启动Tomcat访问不了首页
  7. 【问题】tableView的每组的头部不不能滚动的解决方案
  8. php+mysql+pdo连接数据库
  9. web学习总结之布局
  10. python-希尔排序
  11. SpringBoard 无法启动应用程序(错误:-3)
  12. ----------- Rootkit 核心技术之绕过 IopParseDevice() 调用源检测逻辑 ---------------
  13. Spring 4.x (二)
  14. Shell脚本——特殊符号
  15. Activity的task任务栈
  16. *args **kwargs
  17. SQL 添加索引
  18. JavaScript面向对象的三大特性
  19. 【Python31--pickle函数】
  20. C#开发VS LUA开发

热门文章

  1. 【mysql】linux, mac mysql数据库root 密码忘记修改
  2. python中实现格式化输出 %用法
  3. Mac VMware fusion nat 外网映射
  4. flask中的上下文_请求上下文和应用上下文
  5. MFC CString 和int相互转化
  6. 【LeetCode】Count and Say(报数)
  7. [android开发篇]使用系统权限流程
  8. hdu2055
  9. 有关git的配置
  10. request.getContextPath是为了解决相对路径的问题,可返回站点的根路径