辣鸡OI毁我青春

Description

The alphabet of Freeland consists of exactly N letters. Each sentence of Freeland language (also known as Freish) consists of exactly M letters without word breaks. So, there exist exactly N^M different Freish sentences.

But after recent election of Mr. Grass Jr. as Freeland president
some words offending him were declared unprintable and all sentences
containing at least one of them were forbidden. The sentence S contains a
word W if W is a substring of S i.e. exists such k >= 1 that S[k] =
W[1], S[k+1] = W[2], ...,S[k+len(W)-1] = W[len(W)], where k+len(W)-1
<= M and len(W) denotes length of W. Everyone who uses a forbidden
sentence is to be put to jail for 10 years.

Find out how many different sentences can be used now by freelanders without risk to be put to jail for using it.

Input

The
first line of the input file contains three integer numbers: N -- the
number of letters in Freish alphabet, M -- the length of all Freish
sentences and P -- the number of forbidden words (1 <= N <= 50, 1
<= M <= 50, 0 <= P <= 10).

The second line contains exactly N different characters -- the
letters of the Freish alphabet (all with ASCII code greater than 32).

The following P lines contain forbidden words, each not longer than
min(M, 10) characters, all containing only letters of Freish alphabet.

Output

Output the only integer number -- the number of different sentences freelanders can safely use.

Sample Input

2 3 1
ab
bb

Sample Output

5

Source

Northeastern Europe 2001, Northern Subregion

把P串建成AC自动机,在trie树上动规。f[i][j]表示字符串长度为i,目前走到ac自动机的j结点时的可行方案数。

循环i,j,枚举尝试j可到的下一个结点k,若自动机上k结点不是终止结点,则可以转移:f[i+1][k]+=f[i][j]

P串中可能会出现一个包含另一个的情况。处理方法:在建树时,若fail[x]是终止结点,x也标记成终止结点。

加了个map映射来缩小树规模,其实直接用普通int数组映射也行

 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
int n,m,p;
char alpha[];
map<char,int> mp;
struct num
{
int l,a[];
num operator + (const num &x) const
{
num ans;
int len;
memset(ans.a,,sizeof(ans.a));
for (int i=;i<=l||i<=x.l;i++)
{
ans.a[i]+=a[i]+x.a[i];
ans.a[i+]+=ans.a[i]/;
ans.a[i]%=;
}
if (l<x.l) len=x.l+;
else len=l+;
while (!ans.a[len]&&len) len--;
ans.l=len;
return ans;
}
}f[][],ans;
void prt(num x)
{
printf("%d",x.a[x.l]);
for (int i=x.l-;i>=;i--)
{
int y=x.a[i];
if (y<) printf("");
if (y<) printf("");
if (y<) printf("");
printf("%d",y);
}
}
struct ACa
{
int next[][];
int fail[],end[];
int root,cnt;//根结点,计数器
int newnode(){
for(int i=;i<n;i++){
next[cnt][i]=-;//子结点设为空
}
end[cnt++]=;//以该结点结束的串数
return cnt-;
}
void clear(){//初始化
cnt=;//结点数重置
root=newnode();//初始化根结点 //完成后root=0
}
void insert(char c[]){
int now=root;
int len=strlen(c);
for(int i=;i<len;i++){
if(next[now][mp[c[i]]]==-)//如果对应结点未建立,添加结点
next[now][mp[c[i]]]=newnode();
now=next[now][mp[c[i]]];//否则沿结点继续
}
end[now]++;
}
void build(){
queue<int>q;
fail[root]=root;
for(int i=;i<n;i++){
if(next[root][i]==-)
next[root][i]=root;
else{//有结点则入队
fail[next[root][i]]=root;
q.push(next[root][i]);
}
end[next[root][i]]|=end[next[root][i]];
}
while(!q.empty())
{
int now=q.front();
q.pop();
end[now]|=end[fail[now]];
for(int i=;i<n;i++){
if(next[now][i]==-)
next[now][i]=next[fail[now]][i];//链接到fail指针对应结点的后面
else{
fail[next[now][i]]=next[fail[now]][i];
q.push(next[now][i]);
}
}
}
return;
}
};
ACa ac;
int main(){
scanf("%d%d%d\n",&n,&m,&p);
int i,j;int k;
scanf("%s",alpha);
for(i=;i<n;i++)mp[alpha[i]]=i;//
char c[];
ac.clear();
for(i=;i<=p;i++){
scanf("%s",c);
ac.insert(c);
}
ac.build();
f[][].l=;
f[][].a[]=;
for(i=;i<m;i++){//字符串长度
for(j=;j<ac.cnt;j++)//ac自动机的结点
for(k=;k<n;k++){
int to=ac.next[j][k];//下一步尝试到达的结点
if(ac.end[to])continue;
f[i+][to]=f[i+][to]+f[i][j];
}
}
for(i=;i<ac.cnt;i++)if(!ac.end[i])ans=ans+f[m][i];
prt(ans);
return ;
}

POJ 1625 Censored!的更多相关文章

  1. POJ 1625 Censored!(AC自动机+DP+高精度)

    Censored! Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 6956   Accepted: 1887 Descrip ...

  2. POJ 1625 Censored!(AC自动机+高精度+dp)

    http://poj.org/problem?id=1625 题意: 给出一些单词,求长度为m的串不包含这些单词的个数. 思路: 这道题和HDU 2243和POJ 2778是一样的,不同的是这道题不取 ...

  3. POJ 1625 Censored! [AC自动机 高精度]

    Censored! Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 9793   Accepted: 2686 Descrip ...

  4. POJ 1625 Censored! (AC自己主动机 + 高精度 + DP)

    题目链接:Censored! 解析:AC自己主动机 + 高精度 + 简单DP. 字符有可能会超过128.用map映射一下就可以. 中间的数太大.得上高精度. 用矩阵高速幂会超时,简单的DP就能解决时间 ...

  5. POJ 1625 Censored ( Trie图 && DP && 高精度 )

    题意 : 给出 n 个单词组成的字符集 以及 p 个非法串,问你用字符集里面的单词构造长度为 m 的单词的方案数有多少种? 分析 :先构造出 Trie 图方便进行状态转移,这与在 POJ 2278 中 ...

  6. POJ 1625 Censored!(大数+DP)

    题目链接 这题,真心木啥意思,就是数据里貌似字符有负数,注意gets读入.. #include <iostream> #include <cstring> #include & ...

  7. POJ 1625 Censored!(AC自动机->指针版+DP+大数)题解

    题目:给你n个字母,p个模式串,要你写一个长度为m的串,要求这个串不能包含模式串,问你这样的串最多能写几个 思路:dp+AC自动机应该能看出来,万万没想到这题还要加大数...orz 状态转移方程dp[ ...

  8. POJ 1625 Censored!(AC自动机 + DP + 大数 + 拓展ASCII处理)题解

    题意:给出n个字符,p个病毒串,要你求出长度为m的不包含病毒串的主串的个数 思路:不给取模最恶劣情况$50^{50}$,所以用高精度板子.因为m比较小,可以直接用DP写. 因为给你的串的字符包含拓展A ...

  9. Match:Censored!(AC自动机+DP+高精度)(POJ 1625)

     Censored! 题目大意:给定一些字符,将这些字符组成一个固定长度的字符串,但是字符串不能包含一些禁词,问你有多少种组合方式. 这是一道好题,既然出现了“一些”禁词,那么这题肯定和AC自动机有点 ...

随机推荐

  1. php安全配置记录

    Php环境部署完成后,通常我们会进行一些安全设置.除了熟悉各种PHP漏洞外,还可以通过配置php.ini来加固PHP的运行环境.PHP官方也曾经多次修改php.ini的默认设置. 接下来,推荐php. ...

  2. 混合语言编程:启用CLR(公共语言运行时编译)让C#调用C++

    前言 关于混合C#和C++的编程方式,本人之前写过一篇博客(参见混合语言编程:C#使用原生的Directx和OpenGL),在之前的博客中,介绍了在C#的Winform和WPF下使用原生的Direct ...

  3. 微软职位内部推荐-SW Engineer II for Skype

    微软近期Open的职位: We are the Skype Beijing team. Skype division drives the communications strategy for Mi ...

  4. [资源]PHP使用消息队列

    利用PHP操作Linux消息队列完成进程间通信 基于HTTP协议的轻量级开源简单队列服务:HTTPSQS[原创] Redis队列——PHP操作简单示例 入队操作 <?php $redis = n ...

  5. git流程及操作

    一.git认识 git是什么?Git是目前世界上最先进的分布式版本控制系统 二.git安装 三.git创建本地库 1.首先选择e盘新建一个文件夹如douban 2.点击鼠标右键,执行Git Bash ...

  6. Linux 基础入门(新版)”实验报告一~十二

    实验报告 日期: 2015年9月15日 一.实验的目的与要求 熟练地使用 Linux,本实验介绍 Linux 基本操作,shell 环境下的常用命令. 二.主要内容 1.Linux 基础入门& ...

  7. Spring中@Component注解,@Controller注解详解

    在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成 Service层,Controll ...

  8. 你的C#代码是怎么跑起来的(一)

    写了那么多C#代码,大家有没有想过自己写的代码编译后的可执行文件内部是什么样子,是怎样在系统上运行的? 编译成exe,然后双击exe文件运行,这中间到底发生了些什么呢,这篇先来剖析下exe内部的样子: ...

  9. IT男的”幸福”生活"续8

    有段时间没写了,还是有点怀念的.   生活不记录下,怕真地会忘.. 以往的种种,时时刻刻回荡在我的脑海中,  最近看着孩子生活照, 猛得回首我便回了到了 续8. …… 坐在回去的公交车上,看着前面两M ...

  10. WPF中RadioButton的分组

    当界面上出现多组Radiobutton时,将所有的Radiobutton写在同一个Grid里面,导致系统认为所有的Radiobutton是同一组,造成选择混乱,解决的方法: 1.要为属于同个组的Rad ...