HDU3595 Computer Virus on Planet Pandora (AC自动机)
来源:互联网 发布:淘宝人生有声小说收听 编辑:程序博客网 时间:2024/06/04 19:32
HDU3595 Computer Virus on Planet Pandora
原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=3695
题意:
多组数据。
每组数据给n个模式串和一个主串(长度<=q),输出有多少个模式串在主串中出现过(倒着也算)。主串给的形式是压缩格式。
数据范围
T<= 10, 0 < n <= 250,0 < q <= 5,100,000
题解:
裸AC自动机。
倒着怎么办?直接再把主串倒着跑一边就行。跑过的把isword设成零。
不用指针之后真是相当舒爽。
代码:
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int N=5100010;const int M=500010; queue<int> Q;int T,n,ch[M][26];char s[N],t[N],str[1095];int last[N],fail[N],tail=0,root=0,isword[N],ans=0;void init(){ memset(ch,0,sizeof(ch)); memset(last,0,sizeof(last)); memset(fail,0,sizeof(fail)); memset(isword,0,sizeof(isword)); tail=0; ans=0;}void insert(){ int len=strlen(str); int p=0; for(int i=0;i<len;i++) { int c=str[i]-'A'; if(!ch[p][c]) ch[p][c]=++tail; p=ch[p][c]; } isword[p]++;}void getfail(){ while(!Q.empty()) Q.pop(); for(int i=0;i<26;i++) { if(ch[0][i]) { fail[ch[0][i]]=last[ch[0][i]]=0; Q.push(ch[0][i]); } } while(!Q.empty()) { int top=Q.front(); Q.pop(); for(int i=0;i<26;i++) { if(!ch[top][i]) {ch[top][i]=ch[fail[top]][i]; continue;} int now=ch[top][i]; fail[now]=ch[fail[top]][i]; if(isword[fail[now]]) last[now]=fail[now]; else last[now]=last[fail[now]]; Q.push(now); } }}int upcomp(){ int len=strlen(t); int now=0; int i=0; while(i<len) { if(t[i]=='[') { int num=0; i++; while(t[i]>='0'&&t[i]<='9') num=num*10+t[i++]-'0'; for(int j=1;j<=num;j++) s[now++]=t[i]; i++; } else if(t[i]>='A'&&t[i]<='Z') s[now++]=t[i]; i++; } return now;}void tadd(int x){ while(x) { ans+=isword[x]; isword[x]=0; int tmp=x; x=last[x]; last[tmp]=0; }}void solve(int len){ int p=0; for(int i=0;i<len;i++) { int c=s[i]-'A'; while(!ch[p][c]&&p) p=fail[p]; p=ch[p][c]; if(p) { if(isword[p]) {tadd(p); last[p]=0;} else {tadd(last[p]); last[p]=0;} } } p=0; for(int i=len-1;i>=0;i--) { int c=s[i]-'A'; while(!ch[p][c]&&p) p=fail[p]; p=ch[p][c]; if(p) { if(isword[p]) {tadd(p); last[p]=0;} else {tadd(last[p]); last[p]=0;} } }}int main(){ scanf("%d",&T); while(T--) { init(); scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%s",str); insert(); } getfail(); scanf("%s",t); int len=upcomp(); solve(len); printf("%d\n",ans); } return 0;}
阅读全文
0 0
- HDU3595 Computer Virus on Planet Pandora (AC自动机)
- FZU 2005 Computer Virus on Planet Pandora (AC自动机)
- HDU 3695 Computer Virus on Planet Pandora(AC自动机)
- HDU 3695 Computer Virus on Planet Pandora (AC自动机)
- HDU - 3695 - Computer Virus on Planet Pandora(AC自动机)
- HDU3695 Computer Virus on Planet Pandora AC自动机模板
- HDU 3695 Computer Virus on Planet Pandora(AC自动机)
- hdu 3695 Computer Virus on Planet Pandora AC自动机
- hdu 3695 Computer Virus on Planet Pandora ac自动机
- HDU 3695 Computer Virus on Planet Pandora AC自动机
- 【AC自动机】HDU 3695 Computer Virus on Planet Pandora 裸题
- hdu 3695 Computer Virus on Planet Pandora(AC自动机)
- HDU 3695 Computer Virus on Planet Pandora (AC自动机)
- HDU3695 Computer Virus on Planet Pandora AC自动机
- hdoj 3695 Computer Virus on Planet Pandora 【AC自动机】
- HDU 3695 Computer Virus on Planet Pandora AC自动机
- HDU - 3695 Computer Virus on Planet Pandora AC自动机+优化
- HDU 3695 Computer Virus on Planet Pandora AC自动机
- RecyclerView源码分析
- 10分钟快速集成tinker
- 到底什么是MVC?
- 回顾+纪念:离开帝都的第一年(转)
- Android Runtime Resource Overlay
- HDU3595 Computer Virus on Planet Pandora (AC自动机)
- TCP/IP协议概述
- 使用postMan实现接口测试以及接口自动化回归测试(http协议)
- Java8中List的排序
- 三十而立再出发
- java中equals方法的用法以及==的用法(转)
- python里使用string.Template怎么避免抛出异常
- susy 使用步骤
- Matlab图像数据类型unit8,double关系