zoukankan      html  css  js  c++  java
  • hdu3695 ac自动机入门

    Computer Virus on Planet Pandora

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

    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
    3
    2
    AB
    DCB
    DACB
    3
    ABC
    CDE
    GHI
    ABCCDEFIHG
    4
    ABB
    ACDEE
    BBB
    FEEE
    A[2B]CD[4E]F
     
    Sample Output
    0
    3
    2
     
    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’.

     模板题:

      1  #pragma comment(linker, "/STACK:16777216")
      2 #include <iostream>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <algorithm>
      6 #include <math.h>
      7 #include <queue>
      8 #include <string.h>
      9 using namespace std;
     10 
     11 typedef struct abcd
     12 {
     13     abcd *next[26];
     14     int end;
     15     abcd *fail;
     16 }abcd;
     17 int ans;
     18 abcd *inti()
     19 {
     20     abcd *t;
     21     t=(abcd *)malloc(sizeof(abcd));
     22     for(int i=0;i<26;i++)
     23     t->next[i]=NULL;
     24     t->end=0;
     25     t->fail=NULL;
     26     return t;
     27 }
     28 void insert(abcd *t,char z[])
     29 {
     30     if(*z=='')
     31     {
     32         t->end++;
     33         return;
     34     }
     35     if(t->next[*z-'A']==NULL)
     36     t->next[*z-'A']=inti();
     37     insert(t->next[*z-'A'],z+1);
     38 }
     39 void ac(abcd *t)
     40 {
     41     queue<abcd*>a;
     42     while(!a.empty())a.pop();
     43     for(int i=0;i<26;i++)
     44     {
     45         if(t->next[i]!=NULL)
     46         {
     47             t->next[i]->fail=t;
     48             a.push(t->next[i]);
     49         }
     50         else t->next[i]=t;
     51     }
     52     abcd *r,*f;
     53     while(!a.empty())
     54     {
     55         r=a.front();
     56         a.pop();
     57         for(int i=0;i<26;i++)
     58         {
     59             if(r->next[i])
     60             {
     61                 a.push(r->next[i]);
     62                 f=r->fail;
     63                 while(!f->next[i])f=f->fail;
     64                 r->next[i]->fail=f->next[i];
     65             }
     66         }
     67     }
     68 }
     69 void query(abcd *t,char x[])
     70 {
     71     abcd *f,*p=t;
     72     while(*x)
     73     {
     74         while(!p->next[*x-'A'])p=p->fail;
     75         p=p->next[*x-'A'];
     76         f=p;
     77         while(f!=t&&f->end!=-1)
     78         {
     79             ans+=f->end;
     80             f->end=-1;
     81             f=f->fail;
     82         }
     83         x++;
     84     }
     85     x--;
     86     p=t;
     87     while(*x)
     88     {
     89         while(!p->next[*x-'A'])p=p->fail;
     90         p=p->next[*x-'A'];
     91         f=p;
     92         while(f!=t&&f->end!=-1)
     93         {
     94             ans+=f->end;
     95             f->end=-1;
     96             f=f->fail;
     97         }
     98         x--;
     99     }
    100 }
    101 void del(abcd *t)
    102 {
    103     for(int i=0;i<26;i++)
    104     {
    105         if(!t->next[i])
    106         del(t->next[i]);
    107     }
    108     free(t);
    109 }
    110 int main()
    111 {
    112    int tr,n,i;
    113    //cout<<"YES"<<endl;
    114    cin>>tr;
    115    while(tr--)
    116    {
    117        abcd *t;
    118       t=inti();
    119        char z[1009];
    120        cin>>n;
    121        for(i=0;i<n;i++)
    122        {
    123            cin>>z;
    124            insert(t,z);
    125        }
    126        ac(t);
    127        char x[5200000];
    128        int tt=1,r;
    129        char xx;
    130        x[0]='';
    131        xx=getchar();
    132        xx=getchar();
    133        while(xx!='
    ')
    134        {
    135            if(xx!='[')
    136            x[tt++]=xx;
    137            else
    138            {
    139                cin>>r;
    140                xx=getchar();
    141                for(int i=0;i<r;i++)
    142                x[tt++]=xx;
    143                xx=getchar();
    144            }
    145            xx=getchar();
    146        }
    147        x[tt]='';
    148        ans=0;
    149        query(t,x+1);
    150        cout<<ans<<endl;
    151        del(t);
    152    }
    153 }
    View Code
  • 相关阅读:
    【原】AMFObject数据格式详解
    STL总结 (C++)
    git相关项目迁移
    OBS_Classic经典版框架
    windows线程同步的几种方式
    面试题之strcpy / strlen / strcat / strcmp的实现
    python实用技巧
    Python Flask学习笔记之数据库
    Python Flask学习笔记之Web表单
    必应每日壁纸批量下载
  • 原文地址:https://www.cnblogs.com/ERKE/p/3278526.html
Copyright © 2011-2022 走看看