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是一样的,不同的是这道题不取 ...
随机推荐
- 重新学习一次javascript;
每次有项目的时候,总觉得自己什么都不会做,然后做的时候又很简单,一会就做完了,啪啪打脸: 每次别人问的时候,我知道怎么做,但是不知道具体原理,觉得瞬间low了: 想要好好的吧基础掌握一下: 这几天空闲 ...
- Spring的IOC分析(一)
我们学习Spring之前需要对23种java的设计模式的9种有一定的理解,设计模式为了解耦,Spring也是在解耦的方向上设计的,所以设计模式要理解一下,它当中用到了很多. 单例模式(写法很多钟,7种 ...
- VS2008 如何将Release版本设置可以调试的DEBUG版本
VS2008 如何将Release版本设置可以调试的DEBUG版本 只需设置三个部分: 项目->属性->C/C++->General->Debug Information Fo ...
- [国嵌攻略][159][SPI子系统]
SPI 子系统架构 1.SPI core核心:用于连接SPI客户驱动和SPI主控制器驱动,并且提供了对应的注册和注销的接口. 2.SPI controller driver主控制器驱动:用来驱动SPI ...
- vuex的使用
vue现在越来越火,不单单可以写简单的小项目,也可以写大中型的项目.但是项目大了,项目之间的数据传递就会变得复杂,那么问题来了?在一个大型项目中,多个组件要公用同一个或多个数据,我们如何保证每个组件获 ...
- putty 与winscp 区别
https://zhidao.baidu.com/question/377968180.html putty 与winscp 有什么区别, 装了 winscp 可以由 putty 替换么 ? 具体用法 ...
- OpenCV3.4两种立体匹配算法效果对比
以OpenCV自带的Aloe图像对为例: 1.BM算法(Block Matching) 参数设置如下: ) + ) & -; cv::Ptr<cv::StereoBM> b ...
- mybatis_SQL映射(3)
文章摘录自:http://blog.csdn.net/y172158950/article/details/17304645 1. 表关联 a) 嵌套查询(传说中的1+N问题) <resultM ...
- MySQL Command Line Client显示中文的部分为空
一连接数据库的时候就设置如下: 先设置 set names gbk,然后再插入就显示中文
- 静态html制作之psd转html
很多时候我们开发拿到的是psd文件,这个时候如果公司有前端,会帮你制作成静态html, 很多小公司是没有这个配置的,所以只能业务开发的自己上马.可以有多种实现方式: 1.对于简单的切图,可以在线网站切 ...