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是一样的,不同的是这道题不取 ...
随机推荐
- Redis进阶实践之四Redis的基本数据类型
一.引言 今天正式开始了Redis的学习,如果要想学好Redis,必须先学好Redis的数据类型.Redis为什么会比以前的Memchaed等内存缓存软件使用的更频繁,适用范围更广呢?就是因为R ...
- [国嵌笔记][006][Linux文本编辑器]
Linux中常见的文本编辑器有Vi和Emacs Vim有3中工作模式:命令行模式.插入模式.底行模式 1.键入i进入插入模式 2.键入[Esc]退回到命令行模式 3.键入:进入底行模式,再键入wq保存 ...
- 在Android studio模拟器中运行apk文件
菜鸟级玩家比看文. win平台下下载的apk文件,怎么能模拟运行出来. 首先得安装Android SDK,不会的自行百度一下. 接下来,打开AVD模拟器,自己创建一个模拟器(过程自己实践) 然后,将你 ...
- 关于HTTP请求出现 405状态码 not allowed的解决办法
最近学爬虫.发现httppost请求目标网站会出现405 状态码,原因为 Apache.IIS.Nginx等绝大多数web服务器,都不允许静态文件响应POST请求 所以将post请求改为get请求即可
- javascript中window.location.search的用法和作用。
用该属性获取页面 URL 地址: window.location 对象所包含的属性 属性 描述 hash 从井号 (#) 开始的 URL(锚) host 主机名和当前 URL 的端口号 hostnam ...
- 阿里大鱼 阿里云api
阿里短信服务API接入指南及示例 : https://yq.aliyun.com/articles/59928 =========================================== ...
- HierarchyID 数据类型用法
树形层次结构(Hierarchy)经常出现在有结构的数据中,T-SQL新增数据类型HierarchyID, 其长度可变,用于存储层次结构中的路径.HierarchyID表示的层次结构是树形的,由应用程 ...
- Centos7-卸载自带的jdk 安装jdk8
卸载JDK Centos7一般都会带有自己的openjdk,我们一般都回用oracle的jdk,所以要卸载 步骤一:查询系统是否以安装jdk #rpm -qa|grep java 或 #rpm ...
- 浅析final关键字
浅析final关键字 final单词字面意思是"最终的,不可更改的".所以在java中final关键字表示终态,即最终的状态,"这个东西不能被改变". fina ...
- django url路由参数错误
出现错误: TypeError get() got an unexpected keyword argument 'teacher_id 出错原因: view类中,get方法获得了一个多余的额参数,这 ...