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是一样的,不同的是这道题不取 ...
随机推荐
- [国嵌笔记][010][TFTP与NFS服务器配置]
交叉开发 嵌入式软件产生的平台称为宿主机,运行嵌入式软件的平台称为目标机 宿主机一般通过串口.网络.USB.JTAG等方式将软件下载到目标机 网络下载 一般有TFTP和NFS两种方式 tftp服务器 ...
- FTP下载导致Zip解压失败的原因
情形:网关通过FTP下载快钱对账文件时通过Apache下commons-net的commons-net-3.5.jar进行封装,对账文件中有中文和英文的文字,大部分情况下能够下载成功,而且也能解压成功 ...
- Tomcat之URL查找的过程
Tomcat之URL查找的过程 webapps目录: tomcat共享目录.需要共享的本地资源放到此目录中.
- 十二个 ASP.NET Core 例子——配置操作
目录: 简单配置(利用configration 键值读取) 使用选项和配置对象(自定义类绑定配置文件实现读取) IOptionsSnapshot(配置文件更改时也变化) 内存数据放到配置对象中 实体框 ...
- php通过ini_set调用output_compression压缩网页
网页压缩是一种网页优化技术,可以让网页体积缩小后再传输到客户端,从而减少数据传送量,提高速度.这种技术现在使用已经相当普遍,绝大多数网页都使用了这种技术. 网页压缩可以在服务器或空间里通过参数设置启用 ...
- 关于Vue的路由、脚手架笔记
在页面引入vue-router.js文件,开始配置路由 <div id="box"> <ul><li> <a v-link="{ ...
- 手机WebApp是什么?
手机WebApp是基于HTML5+css3开发的,一次开发,可以兼容许多的平台(android/iphone),调用本地功能(比如照相),可以用phonegap去实现,所以,是一个趋势.相比每个平台定 ...
- Angular 4+ HttpClient
个人博客迁移至 http://www.sulishibaobei.com 处: 这篇,算是上一篇Angular 4+ Http的后续: Angular 4.3.0-rc.0 版本已经发布
- mysql 存在索引但不能使用索引的典型场景
mysql 演示数据库:http://downloads.mysql.com/docs/sakila-db.zip 以%开头的LIKE查询不能够利用B-tree索引 explain select * ...
- JavaSE-反射-获取类或者对象的四种方法
1.使用Class类的静态方法Class.forName("xxxx"); 新建一个要想要获取的类 package org.burning.sport.javase.classlo ...