• 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;
    }
    

      

  • 相关阅读:
    BeagleBone Black安装小米随身WiFi驱动方法
    java-ee,ssh整合博文收藏
    BeagleBoneBlack Linux开发相关链接收藏
    Debian7.7 wheezy 中源码安装emacs24
    GLOG使用注意事项
    centos、linux改变ll命令显示颜色
    centos中samba配置后始终连不上的绝招
    中兴电信光纤猫F612管理员密码获取方法
    STM32W108芯片的SWD在IAR7.30版本中不能用
    TRF7960天线参数试验
  • 原文地址:https://www.cnblogs.com/Zhi-71/p/11674194.html
Copyright © 2020-2023  润新知