POJ 1625 Censored! [AC自动机 高精度]
| Time Limit: 5000MS | Memory Limit: 10000K | |
| Total Submissions: 9793 | Accepted: 2686 |
Description
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 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
Sample Input
2 3 1
ab
bb
Sample Output
5
Source
题意:
给出p个模式串,求有多少个长度为m的字符串,其中不包含任何一个模式串为子串
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=,M=,L=,B=1e4;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,m,p,mp[];
char s[N];
struct node{
int ch[],fail,val;
}t[N];
int sz;
void ins(char s[]){
int u=,n=strlen(s+);
for(int i=;i<=n;i++){
int c=mp[s[i]];
if(!t[u].ch[c]) t[u].ch[c]=++sz;
u=t[u].ch[c];
}
t[u].val=;
}
int q[N],head,tail;
void getAC(){
head=tail=;
for(int i=;i<=mp[];i++) if(t[].ch[i]) q[tail++]=t[].ch[i];
while(head!=tail){
int u=q[head++];
t[u].val|=t[t[u].fail].val;
for(int i=;i<=mp[];i++){
int &v=t[u].ch[i];
if(!v) v=t[t[u].fail].ch[i];
else{
t[v].fail=t[t[u].fail].ch[i];
q[tail++]=v;
}
}
}
} struct big{
int size,d[L];
big():size(){memset(d,,sizeof(d));}
bool has(){return size>||(size==&&d[]!=);}
};
big operator +(big a,big b){
int g=,i;
for(i=;;i++){
if(g==&&i>a.size&&i>b.size) break;
int t=g;
t+=i<=a.size?a.d[i]:;
t+=i<=b.size?b.d[i]:;
a.d[i]=t%B;
g=t/B;
}
a.size=i-;
return a;
}
void print(big &a){
printf("%d",a.d[a.size]);
for(int i=a.size-;i>=;i--){
if(a.d[i]<) printf("");
else if(a.d[i]<) printf("");
else if(a.d[i]<) printf("");
printf("%d",a.d[i]);
}
putchar('\n');
} big f[M][N],ans;
void dp(){
f[][].d[]=;
for(int i=;i<m;i++)
for(int j=;j<=sz;j++) if(!t[j].val&&f[i][j].has())
for(int k=;k<=mp[];k++) if(!t[t[j].ch[k]].val)
f[i+][t[j].ch[k]]=f[i+][t[j].ch[k]]+f[i][j];
for(int i=;i<=sz;i++) ans=ans+f[m][i];
print(ans);
}
int main(){
freopen("in","r",stdin);
n=read();m=read();p=read();
scanf("%s",s+);
for(int i=;i<=n;i++) mp[s[i]]=i;mp[]=n;
for(int i=;i<=p;i++) scanf("%s",s+),ins(s);
//for(int i=1;i<=n;i++) printf("mp %c %d\n",s[i],mp[s[i]]);
getAC();
dp();
}
POJ 1625 Censored! [AC自动机 高精度]的更多相关文章
- POJ 1625 Censored!(AC自动机+DP+高精度)
Censored! Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 6956 Accepted: 1887 Descrip ...
- poj 1625 (AC自动机好模版,大数好模版)
题目 给n个字母,构成长度为m的串,总共有n^m种.给p个字符串,问n^m种字符串中不包含(不是子串)这p个字符串的个数. 将p个不能包含的字符串建立AC自动机,每个结点用val值来标记以当前节点为后 ...
- Match:Censored!(AC自动机+DP+高精度)(POJ 1625)
Censored! 题目大意:给定一些字符,将这些字符组成一个固定长度的字符串,但是字符串不能包含一些禁词,问你有多少种组合方式. 这是一道好题,既然出现了“一些”禁词,那么这题肯定和AC自动机有点 ...
- POJ 1625 Censored!(AC自动机->指针版+DP+大数)题解
题目:给你n个字母,p个模式串,要你写一个长度为m的串,要求这个串不能包含模式串,问你这样的串最多能写几个 思路:dp+AC自动机应该能看出来,万万没想到这题还要加大数...orz 状态转移方程dp[ ...
- poj 2278 DNASequnce AC自动机
地址:http://poj.org/problem?id=2778 题目: DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Total S ...
- ZOJ 3494 (AC自动机+高精度数位DP)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3494 题目大意:给定一些被禁止的BCD码.问指定范围内不含有 ...
- POJ 1625 Censored!
辣鸡OI毁我青春 Description The alphabet of Freeland consists of exactly N letters. Each sentence of Freela ...
- DNA Sequence - POJ 2778(AC自动机+矩阵乘法)
题目大意:DNA序列是有 ATGC 组成的,现在知道一些动物的遗传片段有害的,那么如果给出这些有害的片段,能否求出来所有长度为 N 的基因中有多少是不包含这些有害片段的. 分析:也是断断续续做了一 ...
- POJ 1625 Censored!(AC自动机+高精度+dp)
http://poj.org/problem?id=1625 题意: 给出一些单词,求长度为m的串不包含这些单词的个数. 思路: 这道题和HDU 2243和POJ 2778是一样的,不同的是这道题不取 ...
随机推荐
- SpringMVC框架学习笔记(2)——使用注解开发SpringMVC
1.配置web.xml <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.sp ...
- c#中winform窗口的隐藏与显示
最近在做一个C# 的winform客户端程序,要实现在打开新的窗口时将原来打开的窗口关闭,但是想在关闭新打开的窗口是将原来的那个窗口再次打开,在网上查找各种资料,找了很多代码,都是通过窗口.Hide( ...
- JAVA经典算法面试40题及答案
现在是3月份,也是每年开年企业公司招聘的高峰期,同时有许多的朋友也出来找工作.现在的招聘他们有时会给你出一套面试题或者智力测试题,也有的直接让你上机操作,写一段程序.算法的计算不乏出现,基于这个原因我 ...
- Python3 的函数(2)
1.形参和实参 def MyFun(x): return x ** 3 y = 3 print(MyFun(y)) x为形参,y为实参. 2.函数文档 在函数内用单引号引起来的一段文字,在调用函数时不 ...
- Maven打包时去掉项目版本号
Maven打包后,jar或war文件名里带有版本号信息,如projectname0.0.1-SNAPSHOT.jar等,怎么去掉呢? 解决办法: 打开项目pom.xml文件,在<build> ...
- RMQ算法 (ST算法)
概述: RMQ(Range Minimum/Maximum Query),即区间最值查询,是指这样一个问题:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中 ...
- UML图学习之二 类图
类图(ClassDiagrams)是根据系统中的类以及各类之间的关系描述系统的静态视图.类图不仅显示系统内信息的结构,还描述系统内这些信息的行为.类图的一个重要目的是为其他图(如顺序图.交互图)定义一 ...
- CCF系列之I’m stuck!(201312-5)
试题名称: I’m stuck! 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 给定一个R行C列的地图,地图的每一个方格可能是'#', '+', '-', '|', '.', ...
- 利用overflow实现导航栏中常 出现的倒三角下拉小图标
常用网页界面中,导航栏中的倒三角下拉小图标实现,可用overflow: 效果如右图: .Triangle{position:relative;width:280px;height:15px;} ;ov ...
- 如何 Scale Up/Down Deployment?- 每天5分钟玩转 Docker 容器技术(126)
伸缩(Scale Up/Down)是指在线增加或减少 Pod 的副本数.Deployment nginx-deployment 初始是两个副本. k8s-node1 和 k8s-node2 上各跑了一 ...