/*
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. nrf51822裸机教程-SPI(主)

    关于SPI总线的介绍这里就不细说了,网上有很多介绍SPI总线时序的. SPI总线的本质就是一个环形总线结构,在时钟驱动下两个双向移位寄存器进行数据交换. 所以SPI总线的特色就是:传输一字节数据的同时 ...

  2. 低功耗蓝牙4.0BLE编程-nrf51822开发(4)

    蓝牙是一种短距离的通讯方式,它设计的意图是取代电子便携设备之间的有线电缆连接.蓝牙的主要特性是健壮性.低功耗.成本低,它工作于免费的2.4无线传输频段. 蓝牙有两种技术系统:基本速率Basic Rat ...

  3. Solr4.3之检索建议suggest

    原文链接:http://www.656463.com/article/Efm26v.htm 很多才学solr的人,都容易把solr spellcheck和solr suggest混淆,误以为他们是一样 ...

  4. mysql binlog恢复

    MySQL Binary Log也就是常说的bin-log, ,是mysql执行改动产生的二进制日志文件,其主要作用有两个: * 数据回复 * 主从数据库.用于slave端执行增删改,保持与maste ...

  5. Wordpress页面判断

    is_single() 判断是否是文章页面 is_single('2')/is_single(2)判断是否是具体文章(id=2)的页面 is_single('')判断是否是具体文章(标题为Beef S ...

  6. memory_limit的一个bug | 风雪之隅

    原文:memory_limit的一个bug | 风雪之隅 27 Nov 09 memory_limit的一个bug 作者: Laruence( ) 本文地址: http://www.laruence. ...

  7. Celery - Best Practices

    If you've worked with Django at some point you probably had the need for some background processing ...

  8. Swift-08-闭包引起的循环强引用

    循环强引用还会发生在当你将一个闭包赋值给类实例的某个实例,并且这个闭包体中又实用了这个类实例.这个闭包体重可能访问了实例的某个属性,例如self.**,或者闭包中调用了实例的某个方法,例如self.* ...

  9. maven3创建多模块web项目

    实现步骤 创建项目目录 进入“工作空间”目录,创建名为treasure的文件夹,切换至控制台,进入该文件夹. 配置模块 生成各个模块  maven-archetype-quickstart 默认的Ar ...

  10. Chrome控制台调试工具用法

    下面我们来看看console里面具体提供了哪些方法可以供我们平时调试时使用. Console API 当打开 firebug (也包括 Chrome 等浏览器的自带调试工具),window 下面会注册 ...