zoukankan      html  css  js  c++  java
  • hdu2222(AC自动机)

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

    Keywords Search

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 87731    Accepted Submission(s): 30561


    Problem Description
    In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
    Wiskey also wants to bring this feature to his image retrieval system.
    Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
    To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
     
    Input
    First line will contain one integer means how many cases will follow by.
    Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
    Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
    The last line is the description, and the length will be not longer than 1000000.
     
    Output
    Print how many keywords are contained in the description.
     
    Sample Input
    1
    5
    she
    he
    say
    shr
    her
    yasherhs
     
    Sample Output
    3

     题意:给出n个关键字字符串,然后给出一个字符串s,问有多少个关键字字符串出现在字符串s。

    看到这道题,发现这是一道字符串匹配的题,那么很容易想到KMP来解,然而当关键字足够多的时候,那么肯定超时,用trie树也一样,这时我们需要一个新的算法来解决这个问题了:AC自动机。

    这里给出一篇关于AC自动机的博客吧:https://bestsort.cn/2019/04/28/402/

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1e6+5;
    int tre[maxn][30];
    int sum[maxn];
    char ch1[maxn];
    char ch[105];
    queue<int> que;
    int fail[maxn];
    void insert(char * ch,int & cnt){
    	int rt=0;
    	int len=strlen(ch);
    	for(int i=0;i<len;i++){
    		int tem=ch[i]-'a';
    		if(tre[rt][tem]==0){
    			tre[rt][tem]=++cnt;	
    		}
    		rt=tre[rt][tem];
    	}
    	sum[rt]++;
    	return ;
    }
    void getfail(){
    	while(!que.empty())que.pop();
    	int now=0;
    	for(int i=0;i<26;i++){
    		if(tre[now][i]==0)continue;
    		que.push(tre[now][i]);
    	}
    	while(!que.empty()){
    		now=que.front();
    		que.pop();
    //		cout<<now<<endl;
    		for(int i=0;i<26;i++){
    			if(tre[now][i]!=0){
    				fail[tre[now][i]]=tre[fail[now]][i];
    				que.push(tre[now][i]);
    			}
    			else{
    				tre[now][i]=tre[fail[now]][i];
    			}
    		}
    	}
    //	puts("YES");
    }
    int query(char * ch){
    	int len=strlen(ch);
    	int ans=0;
    	int now=0;
    	for(int i=0;i<len;i++){
    		now=tre[now][ch[i]-'a'];
    		for(int j=now;j&&sum[j]!=-1;j=fail[j]){
    			ans+=sum[j];
    			sum[j]=-1;
    		}
    	}
    	return ans;
    }
    int main(){
    	int t;
    	scanf("%d",&t);
    	while(t--){
    		int n;
    		int cnt=0;
    		memset(sum,0,sizeof(sum));
    		memset(fail,0,sizeof(fail));
    		memset(tre,0,sizeof(tre));
    		scanf("%d",&n);
    		for(int i=0;i<n;i++){
    			scanf("%s",ch);
    			insert(ch,cnt);
    		}
    		getfail();
    		scanf("%s",ch1);
    		printf("%d
    ",query(ch1));
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    还有更简单的不重复随机数生成方法吗?
    SqlServer数据插入性能小记
    html页面滚动时元素错位解决方案
    为Web页中的Table对象创建一个映射表
    js实现的快速排序
    webkit内核的浏览器为什么removeAttribute('style')会失效?
    setAttribute第三个参数
    Windows转到linux中,文件乱码,文件编码转换
    查看端口的占用
    sndfile
  • 原文地址:https://www.cnblogs.com/Zhi-71/p/11674194.html
Copyright © 2011-2022 走看看