C++之路进阶——hdu2222(Keywords Search)
/*
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 47794 Accepted Submission(s): 15228
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
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm> using namespace std; char s[],m[];
int T,n,sz,ans;
int a[][]/*字典树*/ ,q[],point[],danger[]/*单词结束标志*/;
bool mark[]; int ins()
{
int now=,l=strlen(s);
for (int i=;i<l;i++)
{
int t=s[i]-'a'+;
if (a[now][t]) now=a[now][t];
else now=a[now][t]=++sz;
}
danger[now]++;
} int acmach()
{
q[]=;point[]=;
int now,w=,t=;
while (t<w)
{
now=q[t++];
for (int i=;i<=;i++)
{
if (!a[now][i]) continue;
int k=point[now];
while (!a[k][i]) k=point[k];
point[a[now][i]]=a[k][i];
q[w++]=a[now][i];
}
}
} int solve()
{
int k=,l=strlen(m);
for(int i=;i<l;i++)
{
mark[k]=;
int t=m[i]-'a'+;
while(!a[k][t]) k=point[k];
k=a[k][t];
if(!mark[k])
for(int j=k;j;j=point[j])
{
ans+=danger[j];
danger[j]=;
}
}
printf("%d\n",ans);
} int main()
{
scanf("%d",&T);
while(T--)
{
ans=;
sz=;
scanf("%d",&n);
for(int i=;i<=;i++)a[][i]=;
for (int i=;i<n;i++)
{
scanf("%s",s);
ins();
}
scanf("%s",m);
acmach();
solve();
for(int i=;i<=sz;i++)
{
point[i]=danger[i]=mark[i]=;
for(int j=;j<=;j++)
a[i][j]=;
}
}
}
C++之路进阶——hdu2222(Keywords Search)的更多相关文章
- HDU2222 Keywords Search 【AC自动机】
HDU2222 Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- AC自动机讲解+[HDU2222]:Keywords Search(AC自动机)
首先,有这样一道题: 给你一个单词W和一个文章T,问W在T中出现了几次(原题见POJ3461). OK,so easy~ HASH or KMP 轻松解决. 那么还有一道例题: 给定n个长度不超过50 ...
- hdu2222 Keywords Search ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu----(2222)Keywords Search(trie树)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU2222 Keywords Search(AC自动机)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu2222 Keywords Search【AC自动机】
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- ACM学习历程—HDU2222 Keywords Search(字典树)
Keywords Search Description In the modern time, Search engine came into the life of everybody like G ...
- HDU2222 Keywords Search —— AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...
随机推荐
- scala手动编译运行
1 Person.scala class Person { var name = "" } object Person { // a one-arg constructor de ...
- 哈哈,好像swift 以后有可能用来开发安卓喔
好像swift 以后有可能用来开发安卓喔,哈哈
- 关于网站的UV分析
一:准备 1.统计的维度 guid tracktime provice 2.key与value的设定 key:date+provice_guid value:NullWritable 3.案例分析 表 ...
- Qt系统托盘
Qt的系统托盘的使用,可比mfc中好多了!他封装了一个专门的QSystemTrayIcon类,建立系统托盘图标.其实在Qt提供的示例程序已经很不错了,$QTDIR\examples\desktop\s ...
- Delphi HOOK示例
本应用程序的Hook: unit UFrmMain; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, ...
- Tomcat执行流程
- 20145211 《Java程序设计》实验报告五————Java网络编程及安全实验报告
实验内容 1.掌握Socket程序的编写: 掌握密码技术的使用: 设计安全传输系统. 实验步骤 这一部分是与我的partner合作的,详见他的博客- [20145326 <Java程序设计> ...
- ArcGIS Engine开发之旅03--ArcGIS Engine中的控件
原文:ArcGIS Engine开发之旅03--ArcGIS Engine中的控件 制图控件,如MapControl.PageLayoutControl,其中MapControl控件主要用于地理数据的 ...
- SQLSERVER 16进制与10进制转换
最近工控项目中遇到的16进制与10进制转换,在.NET中比较容易实现,在SQLSERVER中发现没有直接的转换,尤其是出现超出范围的long负数,即无符号64位整数在sqlserver中的存储.网上找 ...
- C#遍历List并删除某个或者几个元素的方法
C#遍历List并删除某个或者几个元素的方法,你的第一反应使用什么方法实现呢?foreach? for? 如果是foreach,那么恭喜你,你答错了.如果你想到的是用for,那么你只是离成功进了一步. ...