zoukankan      html  css  js  c++  java
  • hdu2222 Keywords Search

    Keywords Search

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


    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
     

    Author
    Wiskey
     

    Recommend
    lcy   |   We have carefully selected several similar problems for you:  2896 3065 2243 2825 3341 
     



    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<queue>
    #define F(i,j,n) for(int i=j;i<=n;i++)
    #define D(i,j,n) for(int i=j;i>=n;i--)
    #define ll long long
    #define pa pair<int,int>
    #define maxn 500005
    using namespace std;
    int tt,n,ans,tot;
    int t[maxn][26],go[maxn],v[maxn];
    bool used[maxn];
    char s[1000005];
    inline void insert()
    {
    	scanf("%s",s);
    	int len=strlen(s),now=1;
    	F(i,0,len-1)
    	{
    		int x=s[i]-'a';
    		if (!t[now][x]) t[now][x]=++tot;
    		now=t[now][x];
    	}
    	v[now]++;
    }
    inline void bfs()
    {
    	queue<int> q;
    	q.push(1);
    	while (!q.empty())
    	{
    		int x=q.front(),y,j;q.pop();
    		F(i,0,25)
    		{
    			j=go[x];
    			while (j&&!t[j][i]) j=go[j];
    			if (t[x][i])
    			{
    				go[y=t[x][i]]=j?t[j][i]:1;
    				q.push(y);
    			}
    			else t[x][i]=j?t[j][i]:1;
    		}
    	}
    }
    inline void calc(int j)
    {
    	while (j&&!used[j])
    	{
    		if (v[j]) ans+=v[j];
    		used[j]=true;
    		j=go[j];
    	}
    }
    inline void find()
    {
    	int len=strlen(s),j=1;
    	F(i,0,len-1)
    	{
    		int x=s[i]-'a';
    		j=t[j][x];
    		calc(j);
    	}
    }
    int main()
    {
    	scanf("%d",&tt);
    	while (tt--)
    	{
    		tot=1;ans=0;
    		memset(t,0,sizeof(t));
    		memset(v,0,sizeof(v));
    		memset(go,0,sizeof(go));
    		memset(used,false,sizeof(used));
    		scanf("%d",&n);
    		F(i,1,n) insert();
    		bfs();
    		scanf("%s",s);
    		find();
    		printf("%d
    ",ans);
    	}
    }
    


  • 相关阅读:
    腾讯测试开发岗一面二面上机编程题
    软件测试工程师职业发展漫谈
    学习 Python,这 22 个包怎能不掌握?
    Loj514「LibreOJ β Round #2」模拟只会猜题意
    后缀数组模板(倍增)
    USACO06DEC 牛奶模式
    BZOJ3680 JSOI2004 平衡点
    loj6278 数列分块入门题2
    loj6277 数列分块入门题1
    Codeforces 383C Propagating tree, 线段树, 黑白染色思想
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6999139.html
Copyright © 2011-2022 走看看