zoj3228(AC自动机进阶)
来源:互联网 发布:sql count用法详解 编辑:程序博客网 时间:2024/05/20 10:13
Little jay really hates to deal with string. But moondy likes it very much, and she's so mischievous that she often gives jay some dull problems related to string. And one day, moondy gave jay another problem, poor jay finally broke out and cried, " Who can help me? I'll bg him! "
So what is the problem this time?
First, moondy gave jay a very long string A. Then she gave him a sequence of very short substrings, and asked him to find how many times each substring appeared in string A. What's more, she would denote whether or not founded appearances of this substring are allowed to overlap.
At first, jay just read string A from begin to end to search all appearances of each given substring. But he soon felt exhausted and couldn't go on any more, so he gave up and broke out this time.
I know you're a good guy and will help with jay even without bg, won't you?
Input
Input consists of multiple cases( <= 20 ) and terminates with end of file.
For each case, the first line contains string A ( length <= 10^5 ). The second line contains an integer N ( N <= 10^5 ), which denotes the number of queries. The next N lines, each with an integer type and a string a ( length <= 6 ), type = 0 denotes substring a is allowed to overlap and type = 1 denotes not. Note that all input characters are lowercase.
There is a blank line between two consecutive cases.
Output
For each case, output the case number first ( based on 1 , see Samples ).
Then for each query, output an integer in a single line denoting the maximum times you can find the substring under certain rules.
Output an empty line after each case.
Sample Input
ab20 ab1 ababababac20 aba1 abaabcdefghijklmnopqrstuvwxyz30 abc1 def1 jmn
Sample Output
Case 111Case 232Case 3110
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#define mem(a) memset((a),0,sizeof(a))using namespace std;const int N=26;struct node{ int num; node *fail; node *next[N]; int len,tmp; int vis[2]; node() { fail=NULL; num=0; len=0; tmp=-1; for(int i=0; i<N; i++) next[i]=NULL; vis[0]=vis[1]=0; }}*ac[600005];int head,tail;node *root;char str[100005][10];int c[100005];char S[100005];int *y[100005];void build(char *s,int pos){ int len=strlen(s),tmp; node *p=root; for(int i=0; i<len; i++) { tmp=s[i]-'a'; if(p->next[tmp]==NULL) p->next[tmp]=new node(); p=p->next[tmp]; } p->num=pos; p->len=len; p->tmp=-1;}void get_fail(){ ac[tail++]=root; while(head!=tail) { node *p=ac[head++]; node *tmp=NULL; for(int i=0; i<N; i++) { if(p->next[i]!=NULL) { if(p==root) p->next[i]->fail=root; else { tmp=p->fail; while(tmp!=NULL) { if(tmp->next[i]!=NULL) { p->next[i]->fail=tmp->next[i]; break; } tmp=tmp->fail; } if(tmp==NULL) p->next[i]->fail=root; } ac[tail++]=p->next[i]; } } }}int ans0[100005],ans1[100005];void query(char *str){ node *p=root; int tmp,len=strlen(str); for(int i=0; i<len; i++) { tmp=str[i]-'a'; while(p->next[tmp]==NULL&&p!=root) p=p->fail; p=p->next[tmp]; if(p==NULL) p=root; node *exp=p; while(exp!=root) { if(exp->num!=0) { exp->vis[0]++; if(exp->tmp==-1||exp->tmp+exp->len<=i) { exp->vis[1]++; exp->tmp=i; } } exp=exp->fail; } }}int get_ans(char *s,int flag){ int i=0,j; node *p=root; while(s[i]) { j=s[i++]-'a'; p=p->next[j]; } return p->vis[flag];}void del(node *root){ int i; if(root!=NULL) { for(i=0; i<26; i++) { if(root->next[i]!=NULL) del(root->next[i]); } free(root); }}int main(){ int n,con=1; while(~scanf("%s",S)) { scanf("%d",&n); root=new node(); head=tail=0; for(int i=1; i<=n; i++) { scanf("%d %s",&c[i],str[i]); build(str[i],i); } mem(ans0); mem(ans1); get_fail(); query(S); printf("Case %d\n",con++); for(int i=1; i<=n; i++) printf("%d\n",get_ans(str[i],c[i])); printf("\n"); del(root); } return 0;}
- zoj3228(AC自动机进阶)
- AC自动机zoj3228
- ZOJ3228【AC自动机】
- [ac自动机]zoj3228 Searching the String
- ZOJ3228---Searching the String(AC自动机)
- zoj3228 Searching the String AC自动机
- AC自动机 zoj3228 Searching the String
- 【ZOJ3228】白濑肆の完美算法教室补番作,0.7%达成 白濑肆×AC自动机
- 自动机理论(AC自动机)
- AC-自动机(AC-Automachine)
- hdu2222,(ac自动机)
- AC自动机(hdu4057)
- hdu2896(AC自动机)
- hdu3065(AC自动机)
- hdu2222(AC自动机)
- bzoj3172(ac自动机)
- HDU2222(AC自动机)
- AC 自动机(模板)
- 第四周项目3①-小学生算术能力测试系统
- CF_6D_LizardsAndBasements_2
- STM32 串口采用DMA方式收发
- GDAL库进度信息编写示例
- 分享一个linux指令手册指南
- zoj3228(AC自动机进阶)
- shell教程三:基本语法
- SecureCRT远程连接到虚拟机Ubuntu Linux上
- Spring 3.x中三种Bean配置方式比较
- 关键帧的边缘检测
- 共享内存 与 mmap
- Spring--AOP的实现
- IT行业导览-第6章-转行
- centos7.2环境编译安装mysql5.5.48