hdu 3695 ac自动机
2014-10-11 22:57
218 查看
Computer Virus on Planet Pandora
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/128000 K (Java/Others)Total Submission(s): 2719 Accepted Submission(s): 756
[align=left]Problem Description[/align]
Aliens on planet Pandora also write computer programs like us. Their programs only consist of capital letters (‘A’ to ‘Z’) which they learned from the Earth. On
planet Pandora, hackers make computer virus, so they also have anti-virus software. Of course they learned virus scanning algorithm from the Earth. Every virus has a pattern string which consists of only capital letters. If a virus’s pattern string is a substring
of a program, or the pattern string is a substring of the reverse of that program, they can say the program is infected by that virus. Give you a program and a list of virus pattern strings, please write a program to figure out how many viruses the program
is infected by.
[align=left]Input[/align]
There are multiple test cases. The first line in the input is an integer T ( T<= 10) indicating the number of test cases.
For each test case:
The first line is a integer n( 0 < n <= 250) indicating the number of virus pattern strings.
Then n lines follows, each represents a virus pattern string. Every pattern string stands for a virus. It’s guaranteed that those n pattern strings are all different so there
are n different viruses. The length of pattern string is no more than 1,000 and a pattern string at least consists of one letter.
The last line of a test case is the program. The program may be described in a compressed format. A compressed program consists of capital letters and
“compressors”. A “compressor” is in the following format:
[qx]
q is a number( 0 < q <= 5,000,000)and x is a capital letter. It means q consecutive letter xs in the original uncompressed program. For example, [6K] means
‘KKKKKK’ in the original program. So, if a compressed program is like:
AB[2D]E[7K]G
It actually is ABDDEKKKKKKKG after decompressed to original format.
The length of the program is at least 1 and at most 5,100,000, no matter in the compressed format or after it is decompressed to original format.
[align=left]Output[/align]
For each test case, print an integer K in a line meaning that the program is infected by K viruses.
[align=left]Sample Input[/align]
3 2 AB DCB DACB 3 ABC CDE GHI ABCCDEFIHG 4 ABB ACDEE BBB FEEE A[2B]CD[4E]F
[align=left]Sample Output[/align]
0 3 2 HintIn the second case in the sample input, the reverse of the program is ‘GHIFEDCCBA’, and ‘GHI’ is a substring of the reverse, so the program is infected by virus ‘GHI’.
[align=left]Source[/align]
2010 Asia Fuzhou Regional Contest
#include<stdio.h> #include<string.h> #define M 5100005 #define N 500005 struct node { node *fail; node *next[26]; int count; node() { count=0; memset(next,0,sizeof(next)); fail=NULL; } }*q ; node *root; int head,tail; char str[M],s[M],s1[M]; void insert(char *str) { int i,n; node *p=root; for(i=0;str[i]!='\0';i++) { n=str[i]-'A'; if(p->next ==NULL) p->next =new node(); p=p->next ; } p->count++; } void build_ac_automation() { int i; q[head++]=root; root->fail=NULL; while(head!=tail) { node *temp=q[tail++]; node *p=NULL; for(i=0;i<26;i++) { if(temp->next[i]!=NULL) { if(temp==root) temp->next[i]->fail=root; else { p=temp->fail; while(p!=NULL) { if(p->next[i]!=NULL) { temp->next[i]->fail=p->next[i]; break; } p=p->fail; } if(p==NULL) temp->next[i]->fail=root; } q[head++]=temp->next[i]; } } } } int query(char *str) { int i=0,cnt=0; node *p=root; while(str[i]) { int len=str[i]-'A'; while(p->next[len]==NULL&&p!=root) p=p->fail; p=p->next[len]; p=(p==NULL)?root:p; node *temp=p; while(temp!=root&&temp->count!=-1) { cnt+=temp->count; temp->count=-1; temp=temp->fail; } i++; } return cnt; } int main() { int t,n,i,k,num,j,m,ans; char ss[1005]; scanf("%d",&t); while(t--) { scanf("%d",&n); root=new node(); for(i=1;i<=n;i++) { scanf("%s",ss); insert(ss); } head=tail=0; build_ac_automation(); scanf("%s",str); k=0; for(i=0;str[i]!='\0';i++) { if(str[i]=='[') { i++; num=0; while(str[i]>='0'&&str[i]<='9') { num=num*10+str[i]-'0'; i++; } for(j=1;j<=num;j++) { s[k++]=str[i]; } i++; } else s[k++]=str[i]; } s[k++]='\0'; //printf("%s\n",s); m=strlen(s); j=0; for(i=m-1;i>=0;i--) s1[j++]=s[i]; s1[j++]='\0'; ans=query(s); ans+=query(s1); printf("%d\n",ans); } return 0; }
相关文章推荐
- HDU-3695-ac自动机
- hdu 3695 ac自动机
- hdu 3695 AC自动机模板题
- hdu 3695 AC自动机模板题
- HDU 3695 Computer Virus on Planet Pandora(10年福州 AC自动机)
- 【AC自动机】HDU 3695 Computer Virus on Planet Pandora 裸题
- 【AC自动机】HDU 3695 Computer Virus on Planet Pandora 裸题
- HDU 3695 AC自动机 裸题
- HDU 3695 / POJ 3987 Computer Virus on Planet Pandora(AC自动机)(2010 Asia Fuzhou Regional Contest)
- hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)
- hdu 3695 10 福州 现场 F - Computer Virus on Planet Pandora 暴力 ac自动机 难度:1
- AC自动机 - 多模式串的匹配 --- HDU 3695 Computer Virus on Planet Pandora(模板题)
- HDU 3695:Computer Virus on Planet Pandora(AC自动机裸题,数组实现AC自动机)
- HDU 3695 浅谈AC自动机模式串匹配算法+严格空间控制
- AC自动机 - 多模式串的匹配 --- HDU 3695 Computer Virus on Planet Pandora
- HDU 3065 病毒侵袭持续中 (AC自动机 + hdu有毒)
- Ac自动机_hdu_2222_Keywords Search
- HDU 2222 AC自动机
- hdu 2222 AC自动机模板题(指针版+数组版)
- HDU 6138 Fleet of the Eternal Throne AC自动机||后缀数组