/*
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)的更多相关文章

  1. HDU2222 Keywords Search 【AC自动机】

    HDU2222 Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  2. AC自动机讲解+[HDU2222]:Keywords Search(AC自动机)

    首先,有这样一道题: 给你一个单词W和一个文章T,问W在T中出现了几次(原题见POJ3461). OK,so easy~ HASH or KMP 轻松解决. 那么还有一道例题: 给定n个长度不超过50 ...

  3. hdu2222 Keywords Search ac自动机

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...

  4. HDU2222 Keywords Search [AC自动机模板]

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  5. hdu----(2222)Keywords Search(trie树)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  6. HDU2222 Keywords Search(AC自动机)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  7. hdu2222 Keywords Search【AC自动机】

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  8. ACM学习历程—HDU2222 Keywords Search(字典树)

    Keywords Search Description In the modern time, Search engine came into the life of everybody like G ...

  9. HDU2222 Keywords Search —— AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...

随机推荐

  1. 查看linux库文件32位还是64位

    查看linux库文件32位还是64位 分类: linux2014-09-25 09:46 238人阅读 评论(0) 收藏 举报 objdump -a  *.a objdump -a  *.so

  2. iOS 键盘隐藏

     IOS7 点击空白处隐藏键盘的几种方法   IOS开发中经常要用到输入框,默认情况下点击输入框就会弹出键盘,但是必须要实现输入框return的委托方法才能取消键盘的显示,对于用户体验来说很不友好,我 ...

  3. 介绍UDF,以及完成大小写的转换

    一:概述 1.UDF 用户自定义函数,用java实现自定义的需求 2.UDF的类型 udf:一进一出 udaf:多进一出 udtf:一进多出 3.udf的实现步骤 继承UDF类 实现evaluate的 ...

  4. (leetcode)Minimum Size Subarray Sum

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  5. webKit和chromium的文章地址

     http://blog.csdn.net/column/details/yongsheng.html?&page=1 

  6. A Word (Or Two) On Quality

    In the world of interactive project management the promise of quality has become cliché. Quality is ...

  7. typedef 和 const

    1. typedef 允许你为各种数据类型定义新名字 #include <stdio.h>typedef char *ptr_to_char; //这让我看起来,好奇怪,不好读void m ...

  8. 微信支付开发(12) 认清微信支付v2和v3

    微信支付现在分为v2版和v3版 2014年9月10号之前申请的为v2版,之后申请的为v3版. V2版中的参数有AppIDAppSecret支付专用签名串PaySignKey商户号PartnerID初始 ...

  9. SSH项目整合教学Eclipse搭建SSH(Struts2+Spring3+Hibernate3)

    这篇博文的目的 尝试搭建一个完整的SSH框架项目. 给以后的自己,也给别人一个参考. 读博文前应该注意: 本文提纲:本文通过一个用户注册的实例讲解SSH的整合.创建Struts项目,整合Hiberna ...

  10. [BS] 小知识点总结-05

    [BS] 小知识点总结-05 1. 不论UIWindow的rootViewController是navC.tabBarC还是VC,也不管modalVC和rootVC中间隔着多少个VC,但是modal出 ...