zoukankan      html  css  js  c++  java
  • HDU 2222

    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

    做题还是记录一下比较好 不然 你也不知道自己到底写过什么题目。。
    直接上AC自动机即可,还是比较容易理解 bfs保证了最长的相同后缀。构造的并非AC自动机而是trie图...

    int T,n,cnt;
    char a[MAXN];
    int t[MAXN][26],vis[MAXN],fail[MAXN],q[MAXN],mark[MAXN];
    inline void insert(int x)
    {
    	int len=strlen(a+1);
    	int p=0;
    	rep(1,len,i)
    	{
    		int c=a[i]-'a';
    		if(!t[p][c])
    		{
    			t[p][c]=++cnt;fail[cnt]=0;mark[cnt]=0;
    			memset(t[cnt],0,sizeof(t[cnt]));
    		}
    		p=t[p][c];
    	}
    	vis[x]=p;
    }
    inline void AC()
    {
    	int l=0,r=0;
    	for(int i=0;i<=25;++i)if(t[0][i])q[++r]=t[0][i];
    	while(++l<=r)
    	{
    		int x=q[l];
    		for(int i=0;i<=25;++i)
    		{
    			if(t[x][i])q[++r]=t[x][i],fail[q[r]]=t[fail[x]][i];
    			else t[x][i]=t[fail[x]][i];
    		}
    	}
    	int len=strlen(a+1);
    	int p=0;
    	for(int i=1;i<=len;++i)
    	{
    		int c=a[i]-'a';
    		p=t[p][c];
    		mark[p]=1;
    	}
    	for(int i=r;i>=1;--i)mark[fail[i]]|=mark[i];
    }
    int main()
    {
    	freopen("1.in","r",stdin);
    	scanf("%d",&T);
    	while(T--)
    	{
    		get(n);cnt=0;memset(t[0],0,sizeof(t[0]));
    		rep(1,n,i)scanf("%s",a+1),insert(i);
    		scanf("%s",a+1);AC();
    		int ans=0;
    		for(int i=1;i<=n;++i)if(mark[vis[i]])++ans;
    		put(ans);
    	}
    	return 0;
    }
    
  • 相关阅读:
    JavaScript String常用方法和属性
    JavaScript null 和 undefined
    document.write()
    MyBatis中的@MapKey注解
    Zookeeper实现分布式锁
    zookeeper相关
    二阶段提交和三阶段提交
    代理模式
    模板方法模式
    策略模式
  • 原文地址:https://www.cnblogs.com/chdy/p/12458188.html
Copyright © 2011-2022 走看看