HDU 3695Computer Virus on Planet Pandora

来源:互联网 发布:蓝桥杯c语言历年试题 编辑:程序博客网 时间:2024/06/06 03:44

Computer Virus on Planet Pandora

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


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
Hint
In 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
想法:主要是看懂题意;想法错了,一直wa,不要释放内存
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;#define sz 26#define N 5100010int T,n;char str[N],key[1010],key1[1010],str1[N];struct Node{    Node *fail,*nxt[sz];    int cnt,flag;    Node()    {        fail=NULL;cnt=0,flag=0;        for(int i=0;i<sz;i++)            nxt[i]=NULL;    }} *root;void insert(char *s){    int tmp,len=strlen(s);    Node *p=root;    for(int i=0;i<len;i++)    {        tmp=s[i]-'A';        if(p->nxt[tmp]==NULL)        {            p->nxt[tmp]=new Node();        }        p=p->nxt[tmp];    }    p->cnt=1;    p->flag=1;}int find(char *str){    int tmp,len=strlen(str),ans=0;    Node *p=root;    for(int i=0;i<len;i++)    {        tmp=str[i]-'A';        while(p->nxt[tmp]==NULL&&p!=root)            p=p->fail;        p=p->nxt[tmp];        if(p==NULL) p=root;        Node *q=p;        while(q!=root&&q->cnt>0)         {             if(q->flag==1)             {                 ans+=q->cnt;                 q->cnt=0;                 q->flag=0;             }             else                break;             q=q->fail;         }    }    return ans;}void build_Fail(){    queue<Node *>q;    q.push(root);    while(!q.empty())    {      Node *p=q.front(),*tmp;q.pop();      for(int i=0;i<sz;i++)          if(p->nxt[i]!=NULL)          {              if(p==root) p->nxt[i]->fail=root;              else              {                  tmp=p->fail;                  while(tmp!=NULL)                  {                      if(tmp->nxt[i]!=NULL)                      {                          p->nxt[i]->fail=tmp->nxt[i];                          break;                      }                      tmp=tmp->fail;                  }                  if(tmp==NULL)                    p->nxt[i]->fail=root;              }              q.push(p->nxt[i]);      }    }}int seach(char *s){    int len=strlen(s),i,j,now;    Node *cur=root;    for(int i=0;i<len;i++)    {        now=s[i]-'A';        if(cur->nxt[now]!=NULL)        {            cur=cur->nxt[now];        }        else            return 0;    }    if(cur->cnt)        return 1;    return 0;}int main(){    scanf("%d",&T);    while(T--)    {      root=new Node();      scanf("%d",&n);      for(int i=1;i<=n;i++)      {          scanf("%s",key);          insert(key);      }      build_Fail();      scanf("%s",str1);      int len=strlen(str1);      int j=0;      for(int i=0;i<len;i++)      {          if(str1[i]>='A'&&str1[i]<='Z')          {              str[j++]=str1[i];          }          if(str1[i]=='[')          {              char ss='\0';              int ww=0;              i++;             while(str1[i]!=']')             {               if(str1[i]>='0'&&str1[i]<='9')               {                 ww=ww*10+(str1[i]-'0');               }               else                ss=str1[i];               i++;             }             while(ww--)             {                 str[j++]=ss;             }          }      }      int ans=0;str[j]='\0';      ans+=find(str);      strrev(str);      str[j]='\0';      ans+=find(str);      printf("%d\n",ans);    }    return 0;}



阅读全文
0 0