hdu3695 Computer Virus on Planet Pandora

来源:互联网 发布:php 设置北京时区 编辑:程序博客网 时间:2024/05/17 22:04

Computer Virus on Planet Pandora

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/128000 K (Java/Others)
Total Submission(s): 2208    Accepted Submission(s): 616


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.
 

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.
 

Output
For each test case, print an integer K in a line meaning that the program is infected by K viruses.
 

Sample Input
32ABDCBDACB3ABCCDEGHIABCCDEFIHG4ABBACDEEBBBFEEEA[2B]CD[4E]F
 

Sample Output
032
看起来数据比较大,用ac自动机就可以了!其次,这题正反跑两次自动机就可以了!
#include <stdio.h>#include <string.h>#include <iostream>using namespace std;#define MIN 11000#define MAX 5100000struct node {int cnt,flag;node *next[26],*fail;}tree[MAX],*root,*qu[MAX];int n,ans,ptr,head,tail;char str[MAX*2],dir[MAX*2],str1[MAX*2],str2[MAX*2];node *CreateNode() {for (int i = 0; i < 26; ++i)tree[ptr].next[i] = NULL;tree[ptr].fail = NULL;tree[ptr].cnt = tree[ptr].flag = 0;return &tree[ptr++];}void Initial() {ans = ptr = 0;head = tail = 0;root = CreateNode();}void Insert(char *str) {int i = 0,k;node *p = root;while (str[i]) {k = str[i++] - 'A';if (p->next[k] == NULL)p->next[k] = CreateNode();p = p->next[k];}p->cnt++,p->flag = 1;}void Build_AC() {qu[head++] = root;while (tail < head) {node *p = qu[tail++];for (int k = 0; k < 26; ++k)if (p->next[k]) {if (p == root) p->next[k]->fail = root;else {p->next[k]->fail = p->fail->next[k];if (!p->next[k]->flag) p->next[k]->cnt = 1;//象征性地表示以当前点的所有后缀是否有危险节点}qu[head++] = p->next[k];}else {if (p == root) p->next[k] = root;else p->next[k] = p->fail->next[k];}}}int Query(char *str) {node *p = root;int i = 0,k,cnt = 0;while (str[i]) {k = str[i++] - 'A';p = p->next[k];if (p->cnt) {node *temp = p;while (temp != root) {if (temp->flag) cnt += temp->cnt;temp->cnt = 0,temp = temp->fail;}}}return cnt;}int main(){int i,j,k,t;scanf("%d",&t);while (t--) {scanf("%d",&n);Initial();for (i = 1; i <= n; ++i)scanf("%s",dir),Insert(dir);Build_AC();scanf("%s",str);for(i=0;str[i];i++){}int len=i;for (i=0,j=0; i<len;i++){    if(str[i]==']')continue;    k=0;    if(str[i]=='['){                i++;                for(;i<len;i++){                    if(!(str[i]>='0'&&str[i]<='9'))                    break;                    k=k*10+str[i]-'0';                }    }    if(k)k--;    for(int s=0;s<=k;s++)            str1[j++]=str[i];}str1[j]='\0';for(i=0;i<j;i++)str2[i]=str1[j-i-1];str2[j]='\0';//cout<<str<<endl<<str1<<endl<<str2<<endl;ans = Query(str1);ans += Query(str2);printf("%d\n",ans);}}


原创粉丝点击