• hdu ----3695 Computer Virus on Planet Pandora (ac自动机)


    Computer Virus on Planet Pandora

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


    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’.
     
    Source
     
    题目分析:   给定一些特征码,一个目标串,然后要你求包含的特征码的值,值得注意的是,对于目标串需要正反两次扫一遍,然后就是解析目标串,将[23c]解压成正常的字符串形式就可以了。
     
    代码:
     
     
      1 #include<stdio.h>
      2 #include<stdlib.h>
      3 #include<queue>
      4 #include<iostream>
      5 
      6 using namespace std;
      7 
      8 const int maxn=26;
      9 struct Trie{
     10 
     11   Trie *child[maxn];
     12   Trie * fail ;
     13   int tail;
     14 
     15   //函数不占用内存空间
     16   void init(){
     17       for(int i=0;i<maxn ;i++ )
     18          child[i]=NULL;
     19          fail=NULL;
     20          tail=0;
     21     }
     22 
     23 };
     24 
     25 char var[1010] ,stt[5100010];
     26 char stmp[5100010]={''};
     27 
     28 //构建一个Trie树
     29 void BuildTrie(char st[] , Trie * root){
     30 
     31       Trie * cur ;
     32     for( int i=0; st[i] ; i++ ){
     33         int ps=st[i]-'A';
     34         if(root->child[ps]==NULL){
     35             cur = new Trie;
     36             cur->init();     //初始化
     37             root->child[ps]=cur;
     38         }
     39         root = root->child[ps];
     40     }
     41 
     42     root->tail++;
     43 }
     44 
     45 //构造失败指正
     46 void BuildFail(Trie * root){
     47    queue<Trie *> sav;
     48    Trie * tmp,*cur ;
     49     sav.push(root);
     50     while(!sav.empty()){
     51        tmp = sav.front();
     52        sav.pop();
     53        for( int i=0 ; i<maxn ; i++ ){
     54             if(tmp->child[i]!=NULL){
     55                 if(tmp==root) {
     56                   tmp->child[i]->fail=root;
     57                 }
     58                 else{
     59                     cur =tmp;
     60                    while(cur->fail){
     61                     if(cur->fail->child[i]!=NULL){
     62                        tmp->child[i]->fail = cur->fail->child[i];
     63                         break;
     64                     }
     65                      cur =cur->fail;
     66                    }
     67 
     68                     if(cur->fail==NULL)
     69                        tmp->child[i]->fail = root ;
     70 
     71                 }
     72                 sav.push(tmp->child[i]);
     73             }
     74        }
     75     }
     76 }
     77 
     78 //查询
     79 int Query( char ss[] , Trie *root){
     80 
     81         Trie * tmp=root ,*cur;
     82         int res=0,ps=0;
     83      for(int i=0; ss[i] ;i++){
     84           ps = ss[i]-'A';
     85          while(tmp->child[ps]==NULL&&tmp!=root){
     86                 tmp=tmp->fail;
     87          }
     88           tmp = tmp->child[ps];
     89           if(tmp==NULL) tmp=root;
     90          cur=tmp;
     91         while(cur!=root&&cur->tail>0){
     92             res+=cur->tail;
     93             cur->tail=0;
     94             cur= cur->fail;
     95         }
     96      }
     97     return res ;
     98 }
     99 
    100 //对字符串进行必要的伸展
    101 int   change(char stt[] ,char stmp []){
    102 
    103      int i,j;
    104     for(i=0,j=0; stt[i] ;i++){
    105          if(stt[i]!='['){
    106               stmp[j++]=stt[i];
    107          }else{
    108              int k=0,lvar=0;
    109              while(stt[++i]>='0'&&stt[i]<='9'){
    110                 lvar=lvar*10+(stt[i]-'0');
    111              }
    112              for(k=0;k<lvar;k++){
    113                  stmp[j++]=stt[i];
    114              }
    115              i++;
    116          }
    117     }
    118       stmp[j]='';
    119      return j;
    120 }
    121 
    122 //交换字符
    123 void swap(char * a,char *b){
    124     if(*a!=*b) *a^=*b^=*a^=*b;
    125 }
    126 //对字符串进行反序输入
    127 
    128 void Reverse(char * ss,int len){
    129 
    130     for(int i=0;i<len/2;i++){
    131         swap(ss[i],ss[len-i-1]);
    132     }
    133 }
    134 
    135 int main(){
    136 
    137   int tes,nm,res;
    138   scanf("%d",&tes);
    139   while(tes--){
    140     res=0;
    141     Trie * root = new Trie;
    142     root->init();
    143       scanf("%d",&nm);
    144   while(nm--){
    145         scanf("%s",var);
    146         BuildTrie(var,root);
    147   }
    148       BuildFail(root);
    149       scanf("%s",stt);
    150     int len= change(stt,stmp);
    151     res=Query(stmp,root);
    152       Reverse(stmp,len);
    153     res+=Query(stmp,root);
    154     printf("%d
    ",res);
    155 }
    156   return 0;
    157 }
  • 相关阅读:
    ASP.NET Core 中间件的使用(二):依赖注入的使用
    ASP .Net Core 中间件的使用(一):搭建静态文件服务器/访问指定文件
    附029.Kubernetes安全之网络策略
    C# 9.0中引入的新特性init和record的使用思考
    拥抱云原生,如何将开源项目用k8s部署?
    Angular实战之使用NG-ZORRO创建一个企业级中后台框架(进阶篇)
    Angular实战之使用NG-ZORRO创建一个企业级中后台框架(入门篇)
    Angular入门,开发环境搭建,使用Angular CLI创建你的第一个Angular项目
    (已解决)'ng' 不是内部或外部命令,也不是可运行的程序或批处理文件
    如何免费扩展谷歌网盘的容量
  • 原文地址:https://www.cnblogs.com/gongxijun/p/4478782.html
Copyright © 2020-2023  润新知