辣鸡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. 关于ZeroMQ的信息与文档

    1. 官方文档(英文,很长) http://zguide.zeromq.org/page:all 2. 云风的关于 http://blog.codingnow.com/2011/02/zeromq_m ...

  2. 我为什么反对推荐新人编程C/C++语言入门?

    虽然我接触编程以及计算机时间比较早,但是正式打算转入程序员这个行当差不多是大学第四年的事情 从03年接触计算机,07年开始接触计算机编程, 期间接触过的技术包括 缓冲区溢出(看高手写的shellcod ...

  3. 关于phpmyadmin #1045无法登陆服务器的问题

    修改/home/wwwroot/phpmyadmin/libraries/config.default.php 文件,找到下面两行 $cfg['Servers'][$i]['nopassword'] ...

  4. Download the WDK, WinDbg, and associated tools

    Download the WDK, WinDbg, and associated tools This is where you get your Windows Driver Kit (WDK) a ...

  5. css3 线性渐变和径向渐变

    线性渐变:ie6以下不兼容 径向渐变:只支持firefox.Chrome和Safari <!DOCTYPE html> <html> <head> <meta ...

  6. 我们为什么需要DTO?

    看了几套源码,其中都有用到DTO,这篇文章主要来谈论一下DTO使用的场合及其带来的好处. 在传统的编程中,我们一般都是前台请求数据,发送到Webservice,然后WebService向数据库发出请求 ...

  7. 设计传说 PS零基础精通 Photoshop CC 2015视频教程

           课程目录: 课时1 Photoshop CC 2015 全面技能培训介绍  03:08课时2 1.1 PSCC工作区域及面板自定义 08:47课时3 1.2 图像文件的多种打开方式 0 ...

  8. 北京联想招聘-Android Framework高级工程师(7-10年) 加入qq 群:220486180 或者直接在此 留言咨询

    Job ID #: 45038 Position Title: Android Framework高级工程师 Location: CHN-Beijing Functional Area: Resear ...

  9. iOS 定位精度

    时间 2015-03-19 18:30:59  图灵社区  由于iOS不能直接控制到GPS,一般来说我们都会使用CLLocationManager来获取地理位置信息,我们会使用 manager.des ...

  10. matlab绘制二维图形

    常用的二维图形命令: plot:绘制二维图形 loglog:用全对数坐标绘图 semilogx:用半对数坐标(X)绘图 semilogy:用半对数坐标(Y)绘图 fill:绘制二维多边填充图形 pol ...