首页 > 代码库 > HDU 3695 Computer Virus on Planet Pandora
HDU 3695 Computer Virus on Planet Pandora
Computer Virus on Planet Pandora
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/128000 K (Java/Others)Total Submission(s): 2480 Accepted Submission(s): 688
Problem Description
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.
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.
Input
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.
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.
Output
For each test case, print an integer K in a line meaning that the program is infected by K viruses.
Sample Input
3 2 AB DCB DACB 3 ABC CDE GHI ABCCDEFIHG 4 ABB ACDEE BBB FEEE A[2B]CD[4E]F
Sample Output
0 3 2HintIn 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’.
Source
2010 Asia Fuzhou Regional Contest
解题思路:AC自动机裸题,求正反两个串中单词出现的总数
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #define N 5555555 using namespace std; struct node { node *fail; node *next[26]; int cnt; node () { fail=0; cnt=0; memset(next,0,sizeof(next)); } }; node *root=NULL; void insert(char *str) { int i=0,index; node *p=root; while(str[i]!='\0') { index=str[i]-'A'; if(p->next[index]==NULL) p->next[index]=new node; p=p->next[index]; i++; } p->cnt++; } void build() { int i; root->fail=NULL; queue<node*> q; q.push(root); while(!q.empty()) { node *tmp=q.front(); q.pop(); node *p=NULL; for(i=0;i<26;i++) { if(tmp->next[i]) { if(tmp==root) tmp->next[i]->fail=root; else { p=tmp->fail; while(p) { if(p->next[i]) { tmp->next[i]->fail=p->next[i]; break; } p=p->fail; } if(p==NULL) tmp->next[i]->fail=root; } q.push(tmp->next[i]); } } } } int query(char *str) { int i=0,cnt=0,index; node *p=root; while(str[i]!='\0') { //cout<<i<<endl; index=str[i]-'A'; while(p->next[index]==NULL&&p!=root) p=p->fail; p=p->next[index]; p=(p==NULL)?root:p; node *tmp=p; while(tmp!=root&&tmp->cnt!=-1) { cnt+=tmp->cnt; tmp->cnt=-1; tmp=tmp->fail; } i++; } return cnt; } char s1[N],s2[N]; void input() { int p=0; char c; getchar(); while(c=getchar(),c!='\n') { if(c!='[') s1[p++]=c; else { int x; scanf("%d",&x); c=getchar(); while(x--) s1[p++]=c; getchar(); } } for(int q=0;p>0;q++) s2[q]=s1[--p]; //cout<<s1<<" "<<s2<<endl; } void del(node *root) { for(int i=0;i<26;i++) if(root->next[i]) del(root->next[i]); delete root; } int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int t,n; scanf("%d",&t); char str[1005]; while(t--) { root=new node; memset(s1,'\0',sizeof(s1)); memset(s2,'\0',sizeof(s2)); scanf("%d",&n); while(n--) scanf("%s",str),insert(str); build(); input(); printf("%d\n",query(s1)+query(s2)); del(root); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。