辣鸡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. python将文件写成csv文件保存到本地

    举个例子: import csv import os path='/tmp/' file='test.csv' def generate_csv(path,file): if not os.path. ...

  2. Iron man

    儿子的手办在近期又新增一套钢铁侠,来自于淘宝的玩具推荐,这个推荐也得益于小美和他平日在淘宝商城里的各种玩具浏览,充分体现了现阶段对复仇者联盟成员的喜爱. 一套共六个,有着不同的颜色,但造型基本一致带L ...

  3. Jenkins + NuGet + MSBuild

    Jenkins + NuGet + MSBuild 背景 项目上需要做UWP的自动安装包,在以前的公司接触的是TFS来做自动build. 公司要求用Jenkins来做,别笑话我,之前还真不晓得这个东西 ...

  4. 未能进入中断模式,原因如下:源文件“XXXXXX”不属于正在调试的项目。

    这个问题是由于项目文件位置变动导致的.提示框已经说的比较清楚了. 首先可以尝试[重新生成] ,一般可以解决这个问题了. 我遇到的情况是,设置配置时,不小心取消了生成选择. 所以打开配置管理器,把相关的 ...

  5. [转]在 Eclipse 中嵌入 NASA World Wind Java SDK

    使用此开源 SDK 开发 GIS 应用程序 NASA 开发的开源 World Wind Java (WWJ) SDK 为地理信息系统(Geographic Information Systems,GI ...

  6. python logging 模块

    我有几个项目中使用了 sentry 捕获 ERROR 级别的日志,现在遇到一个问题:本地调试的时候,日志设置中,所有的 handler(包括 root) 都只打到 console 上面,但是本地调试中 ...

  7. PowerDesigner打开设计文件后提示failed to read the fileXXX的解决办法

    擦,一身盗汗.一向的设计信息都在设计图里!竟然坏了,坏了!!!!! 惊.怒.悲 固然可以经由过程数据库当前状况反向工程.然则那么注解.我写的提示这些器材都邑消散. 比来的备份是10天前,恢复也会有必然 ...

  8. windows下git bash显示中文

    1.C:\Program Files\Git\etc\git-completion.bash: alias ls='ls --show-control-chars --color=auto' 说明:使 ...

  9. matlab绘制三维图形

    原文地址:种三维曲面图. 程序如下: [x,y]=meshgrid(-8:0.5:8); z=sin(sqrt(x.^2+y.^2))./sqrt(x.^2+y.^2+eps); subplot(2, ...

  10. UUChart的使用--iOS绘制折线图

    UUChart是一个用于绘制图表的第三方,尤其适合去绘制折线图. 二.下载地址: https://github.com/ZhipingYang/UUChartView 三.使用 第一步.首先我们将下载 ...