zoukankan      html  css  js  c++  java
  • HDU 3695 / POJ 3987 Computer Virus on Planet Pandora

      Computer Virus on Planet Pandora


    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 1353   Accepted: 256

    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

    【题意】:

    给你T组测试数据,每组测试数据有n个模式串,后面跟着一个母串,你需要输出母串包含模式串的个数。(反着也算;不计重复串)

      母串有的需要展开。

    【分析】:

    模式串构建fail树

    母串正着跑一遍匹配,反着跑一遍。(这样既可以降低空间复杂度,又可以提高运行速度)

    • Source Code
    • #include<cstdio>
      #include<cstring>
      #include<algorithm>
      #define Sz 26
      #define m(s) memset(s,0,sizeof s);
      using namespace std;
      const int N=5e5+5,Z=26,F=5.1e6+5;
      int T,n,m,l,ans,cnt=1,tr[N][Z],fail[N],tag[N],q[N];
      bool mark[N];char str[F],s[F];
      //===============================================
      inline void insert(){
      	int now=1;
      	for(int i=0,z;i<l;i++){
      		z=s[i]-'A';
      		if(!tr[now][z]) tr[now][z]=++cnt;
      		now=tr[now][z];
      	}
      	tag[now]++;
      }
      inline void acmach(){
      	for(int i=0;i<Sz;i++) tr[0][i]=1;
      	int h=0,t=1,now,p;q[t]=1;fail[1]=0;
      	while(h!=t){
      		now=q[++h];
      		for(int i=0;i<Sz;i++){
      			if(!tr[now][i]) continue;
      			p=fail[now];
      			while(!tr[p][i]) p=fail[p];
      			p=tr[p][i];
      			fail[tr[now][i]]=p;
      			q[++t]=tr[now][i];
      		}
      	}
      }
      inline void solve(){
      	int now=1;l=strlen(s);
      	for(int z,i=0;i<l;i++){
      		z=s[i]-'A';
      		mark[now]=1;
      		while(!tr[now][z]) now=fail[now];
      		now=tr[now][z];
      		if(!mark[now]){
      			for(int j=now;j;j=fail[j]){
      				if(tag[j]){
      					ans+=tag[j];
      					tag[j]=0;
      				}
      			}
      		}
      	}
      }
      //===============Aho Corasick Automata===============
      inline int translate(){
      	int j=0,x=0;
      	for(int i=0;str[i];i++){
      		if((str[i]>='A'&&str[i]<='Z')
      		 ||(str[i]>='a'&&str[i]<='z')){
      		 	s[j++]=str[i];
      		 }
      		if(str[i]>='0'&&str[i]<='9'){
      			x=x*10+str[i]-'0';
      		}
      		if(str[i]==']'){
      			while(--x) s[j++]=str[i-1];
      		}	
      	}
      	s[j]=0;
      	return j;
      }
      inline void Clear(){
      	ans=0;cnt=1;
      	m(tr);m(tag);m(fail);m(mark);
      }
      int main(){
      	scanf("%d",&T);
      	while(T--){
      		Clear();
      		scanf("%d",&n);
      		for(int i=1;i<=n;i++){
      			scanf("%s",s);l=strlen(s);
      			insert();
      //			reverse(s,s+l);
      //			insert();
      		}
      		acmach();
      		scanf("%s",str);
      		l=translate();
      		solve();
      		reverse(s,s+l);
      		solve();
      		printf("%d
      ",ans);
      	}
      	return 0;
      }

      
  • 相关阅读:
    Grafana+Prometheus监控mysql性能
    性能测试监控平台Grafana的使用
    搭建grafana+telegraf+influxdb服务器性能监控平台
    cocos2d-x jsb 防止触摸事件传递
    web app 相关记录
    如何在Teamcenter中使用PMI?
    浅谈人机工程应用在数字化工艺中的作用
    关于奇葩说
    一些感想
    关于起名
  • 原文地址:https://www.cnblogs.com/shenben/p/6549256.html
Copyright © 2011-2022 走看看