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


  • 相关阅读:
    BigPipe学习研究
    JavaSript模块规范
    WebSocket
    图片链接转成base64
    3000多台式机组装经验分享
    android textview 自动换行 整齐排版
    android 获取所有SD卡目录
    android获取系统信息
    在电脑上用chrome浏览器调试android手机里的网页代码时,无法看到本地加载的js文件
    android 根据坐标返回触摸到的View
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6999139.html
Copyright © 2011-2022 走看看