ZOJ 题目3228 Searching the String(AC自动机,子川匹配个数)
2015-08-08 17:49
495 查看
Searching the String
Time Limit: 7 Seconds
Memory Limit: 129872 KB
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 stringA. 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 integerN (
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
Sample Output
Hint
In Case 2,you can find the first substring starting in position (indexed from 0) 0,2,4, since they're allowed to overlap. The second substring starts in position 0 and 4, since they're not allowed to overlap.
For C++ users, kindly use scanf to avoid TLE for huge inputs.
Author: LI, Jie
Source: ZOJ Monthly, July 2009
题目大意:就是0表示可以覆盖的匹配的个数,1表示不可以覆盖的匹配个数
本来挺水的一道题,不可覆盖的时候就是匹配的位置比上个位置大于等于这个串的长度就行,结果各种MLE,,都快哭了,,从网上找代码也是也是MTE,我以为oj炸了,,各种减啊,,还是MTE,,,最后想到把字典树上分配的内存free掉好了,,提交就过了,,激动坏了,想想之前的可不可以,,找到之前第一次提交的代码,加上del,,也过了
,弱就是弱啊,,整好几个小时就是忘free了,,
,,不多还是多少感觉对ac自动机多了一些理解
之前的ac代码
Time Limit: 7 Seconds
Memory Limit: 129872 KB
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 stringA. 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 integerN (
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
ab 2 0 ab 1 ab abababac 2 0 aba 1 aba abcdefghijklmnopqrstuvwxyz 3 0 abc 1 def 1 jmn
Sample Output
Case 1 1 1 Case 2 3 2 Case 3 1 1 0
Hint
In Case 2,you can find the first substring starting in position (indexed from 0) 0,2,4, since they're allowed to overlap. The second substring starts in position 0 and 4, since they're not allowed to overlap.
For C++ users, kindly use scanf to avoid TLE for huge inputs.
Author: LI, Jie
Source: ZOJ Monthly, July 2009
题目大意:就是0表示可以覆盖的匹配的个数,1表示不可以覆盖的匹配个数
本来挺水的一道题,不可覆盖的时候就是匹配的位置比上个位置大于等于这个串的长度就行,结果各种MLE,,都快哭了,,从网上找代码也是也是MTE,我以为oj炸了,,各种减啊,,还是MTE,,,最后想到把字典树上分配的内存free掉好了,,提交就过了,,激动坏了,想想之前的可不可以,,找到之前第一次提交的代码,加上del,,也过了
,弱就是弱啊,,整好几个小时就是忘free了,,
,,不多还是多少感觉对ac自动机多了一些理解
之前的ac代码
#include<stdio.h> #include<string.h> #include<stdlib.h> char str[100010],key[100010][10]; int op[100010]; int head,tail; struct node { node *fail; node *next[26]; int cnt[2],flag,time,len; node() { fail=NULL; flag=time=0; cnt[0]=cnt[1]=0; for(int i=0;i<26;i++) next[i]=NULL; } }*q[500050]; node *root; void insert(char *s) { int temp,len,i; node *p=root; len=strlen(s); for(i=0;i<len;i++) { temp=s[i]-'a'; if(p->next[temp]==NULL) p->next[temp]=new node(); p=p->next[temp]; } p->flag=1; p->len=len; } void build_ac() { q[tail++]=root; while(head!=tail) { node *p=q[head++]; node *temp=NULL; for(int i=0;i<26;i++) { if(p->next[i]!=NULL) { if(p==root) p->next[i]->fail=root; else { temp=p->fail; while(temp!=NULL) { if(temp->next[i]!=NULL) { p->next[i]->fail=temp->next[i]; break; } temp=temp->fail; } if(temp==NULL) { p->next[i]->fail=root; } } q[tail++]=p->next[i]; } } } } void query() { int t=0; int len=strlen(str); node *p=root,*temp; for(int i=0;i<len;i++) { int x=str[i]-'a'; t++; while(p->next[x]==NULL&&p!=root) p=p->fail; p=p->next[x]; if(p==NULL) { p=root; } temp=p; while(temp!=root) { if(temp->flag) { temp->cnt[0]++; if(t-temp->time>=temp->len) { temp->cnt[1]++; temp->time=t;//最后匹配的位置 } } temp=temp->fail; } } } int find(char s[],int flag) { int len=strlen(s),i,j,now; node *current=NULL; current=root; for(i=0;i<len;i++) { now=s[i]-'a'; if(current->next[now]!=NULL) { current=current->next[now]; } else return 0; } return current->cnt[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 c=0; while(scanf("%s",str)!=EOF) { int n; scanf("%d",&n); int i; head=tail=0; root=new node(); for(i=1;i<=n;i++) { scanf("%d%s",&op[i],key[i]); //len[i]=strlen(key[i]); insert(key[i]); } build_ac(); query(); printf("Case %d\n",++c); for(i=1;i<=n;i++) { int ans=find(key[i],op[i]); printf("%d\n",ans); } printf("\n"); del(root);\ } }
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<iostream> using namespace std; char str[100001],key[7]; bool op[100001]; int pos[100001],ans[100001][2]; int head,tail; struct node { node *fail; node *next[26]; int flag,time,len,id; node() { fail=NULL; flag=time=0; // cnt[0]=cnt[1]=0; id=0; for(int i=0;i<26;i++) next[i]=NULL; } }*q[600005]; node *root; int insert(char *s,int id) { int temp,len,i; node *p=root; len=strlen(s); for(i=0;i<len;i++) { temp=s[i]-'a'; if(p->next[temp]==NULL) p->next[temp]=new node(); p=p->next[temp]; } p->flag=1; p->len=len; if(p->id==0) p->id=id; return p->id; } void build_ac() { q[tail++]=root; while(head!=tail) { node *p=q[head++]; node *temp=NULL; for(int i=0;i<26;i++) { if(p->next[i]!=NULL) { if(p==root) p->next[i]->fail=root; else { temp=p->fail; while(temp!=NULL) { if(temp->next[i]!=NULL) { p->next[i]->fail=temp->next[i]; break; } temp=temp->fail; } if(temp==NULL) { p->next[i]->fail=root; } } q[tail++]=p->next[i]; } } } } void query() { int t=0; int len=strlen(str); node *p=root,*temp; for(int i=0;i<len;i++) { int x=str[i]-'a'; t++; while(p->next[x]==NULL&&p!=root) p=p->fail; p=p->next[x]; if(p==NULL) { p=root; } temp=p; while(temp!=root) { if(temp->flag) { ans[temp->id][0]++; if(t-temp->time>=temp->len) { ans[temp->id][1]++; temp->time=t;//最后匹配的位置 } } temp=temp->fail; } } } /*int find(char s[],int flag) { int len=strlen(s),i,j,now; node *current=NULL; current=root; for(i=0;i<len;i++) { now=s[i]-'a'; if(current->next[now]!=NULL) { current=current->next[now]; } else return 0; } int kk=flag?1:0; return current->cnt[kk]; }*/ 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 c=0; while(scanf("%s",str)!=EOF) { int n; scanf("%d",&n); int i; head=tail=0; root=new node(); memset(ans,0,sizeof(ans)); for(i=1;i<=n;i++) { scanf("%d%s",&op[i],key); //len[i]=strlen(key[i]); pos[i]=insert(key,i); } build_ac(); query(); printf("Case %d\n",++c); for(i=1;i<=n;i++) { int k=op[i]?1:0; printf("%d\n",ans[pos[i]][k]); } printf("\n"); // del(root); } }
相关文章推荐
- scala实现设计模式之建造者模式
- FileUpload上传图片直接浏览显示(没有上传按钮如何上传)
- webpy.org
- SSH框架总结
- jnative 从指针中获得传出值
- FZU - 2125 - 简单等式
- Teaching Mario to play with himself: AI, machine learning, and Super Mario Bros.
- Java核心API -- 12(对象序列化和反序列化)
- 用C#生成KML路径文件(上篇)
- 数据结构学习笔记(二)---单链表
- HDU-OJ-1029 Ignatius and the Princess IV-出现至少(N+1)/2次的数
- 自动布局 01 注意事项
- 《小强升职记》 读书笔记
- java开发微信公众平台备忘
- 3.1html学习之列表
- 汇编语言实现电子闹钟
- uva 11836(两个四面体最短距离)
- Django分页的基本实现办法
- 简单的HTML5在线画板
- [转载] DevOps年中盘点:国外最受欢迎的10篇技术文章