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


  • 相关阅读:
    资料收集
    layui 设计资源——2.0 版本的 Axure 组件包,产品交互设计利器
    照片自动按时间分类助手
    回顾2017,规划2018,展望2019
    coder/programmer engineer Chirf Technology Offcer
    新手开公司创业,这些坑千万别掉进去!
    ASP.NET MVC中的Global.asax文件
    SQLServer(MSSQL)、MySQL、SQLite、Access相互迁移转换工具 DB2DB v1.0
    快速开发之代码生成器(asp.net mvc4 + easyui + knockoutjs)
    PowerDesigner逆向工程导入MYSQL数据库总结
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6999139.html
Copyright © 2011-2022 走看看