• hdu 3065 病毒侵袭持续中 (AC自动机)


    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065

    多测
    卡内存

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<queue>
    using namespace std;
    typedef long long ll;
    
    const int maxn = 100100; 
    
    int n, rt = 0, tot = 0; 
    int ans[maxn];
    
    struct Node{
    	int son[30], en, fail;
    }t[maxn];
    
    char s[1010][55];
    char txt[2000010];
    
    void insert(int num){
    	int p = rt;
    	int len = strlen(s[num]);
    	for(int i = 0 ; i < len ; ++i){
    		if(!t[p].son[s[num][i] - 'A']){
    			t[p].son[s[num][i] - 'A'] = ++tot;
    		}
    		p = t[p].son[s[num][i] - 'A'];
    	} 
    	t[p].en = num;
    }
    
    void build(){
    	queue<int> q; 
    	t[rt].fail = rt;
    	for(int i = 0 ; i <= 25 ; ++i){
    		if(t[rt].son[i]) q.push(t[rt].son[i]);
    	}
    	
    	while(!q.empty()){
    		int u = q.front(); q.pop();
    		for(int i = 0 ; i <= 25 ; ++i){
    			if(t[u].son[i]){
    				t[t[u].son[i]].fail = t[t[u].fail].son[i];
    				q.push(t[u].son[i]);
    			} else{
    				t[u].son[i] = t[t[u].fail].son[i];
    			}
    		}
    	}
    }
    
    void query(){
    	int p = rt;
    	int len = strlen(txt);
    	for(int i = 0 ; i < len ; ++i){
    		if(txt[i] < 'A' || txt[i] > 'Z') p = 0;
    		else p = t[p].son[txt[i] - 'A'];
    		
    		for(int j = p ; j ; j = t[j].fail) {
    			++ans[t[j].en];
    		}
    	}
    }
    
    ll read(){ ll s = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); } return s * f; }
    
    int main(){
    	while(scanf("%d", &n) != EOF && n){
    		memset(ans, 0, sizeof(ans));
    		tot = 0;
    		memset(t, 0, sizeof(t)); 
    		for(int i = 1 ; i <= n ; ++i){
    			scanf("%s", s[i]);
    			insert(i);
    		}
    		
    		build();
    		
    		scanf("%s", txt);
    		query();
    		
    		for(int i = 1 ; i <= n ; ++i){
    			if(ans[i]) {
    				printf("%s: %d
    ", s[i], ans[i]);
    			}
    		}
    	}
    
    	return 0;
    }
    
  • 相关阅读:
    登录远程服务器运行的程序,退出服务器仍然运行的方法
    python爬虫错误
    python将字符转换为字典
    shell中$(( )) 与 $( ) 还有${ }的区别
    JavaBean 开发入门
    反射机制 动态代理
    反射机制的深入应用
    反射机制 反射的应用 ---取得类的结构
    反射机制 CLass类的使用
    JSP 的九大内置对象
  • 原文地址:https://www.cnblogs.com/tuchen/p/14190825.html
Copyright © 2020-2023  润新知