Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 50451    Accepted Submission(s): 16236

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
/* ***********************************************
Author :guanjun
Created Time :2016/5/26 22:17:48
File Name :2222.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long ling
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 250010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std; struct ACauto{
int ch[maxn][];
int sz;
int f[maxn],last[maxn],val[maxn],cnt[maxn];
void init(){
sz=;
memset(ch[],,sizeof ch[]);
memset(cnt,,sizeof cnt);
}
int idx(char c){
return c-'a';
}
void add(char *s,int v){
int u=,len=strlen(s);
for(int i=;i<len;i++){
int c=idx(s[i]);
if(!ch[u][c]){
memset(ch[sz],,sizeof ch[sz]);
val[sz]=;
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]=v;
}
void getfail(){
queue<int>q;
f[]=;
for(int c=;c<;c++){
int u=ch[][c];
if(u){
f[u]=;
q.push(u);
last[u]=;
}
}
while(!q.empty()){
int r=q.front();q.pop();
for(int c=;c<;c++){
int u=ch[r][c];
if(!u){
ch[r][c]=ch[f[r]][c];
continue;
}
q.push(u);
f[u]=ch[f[r]][c];
last[u]=val[f[u]]?f[u]:last[f[u]];
}
}
}
void print(int j){
if(j){
cnt[val[j]]++;
print(last[j]);
}
}
void Find(char *T){
int n=strlen(T);
int j=;
for(int i=;i<n;i++){
int c=idx(T[i]);
while(j&&!ch[j][c])j=f[j];
j=ch[j][c];
if(val[j])print(j);
else if(last[j])print(last[j]);
}
}
}ac;
char s[][],T[];
int BKDRHash(char* s)
{
long long seed=;
long long hash=;
while(*s=='')s++;
while(*s)
{
hash=hash*seed+(*s++);
}
return (hash & 0x7FFFFFFF);
}
map<int ,int>mp;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int t,n;
cin>>t;
while(t--){
ac.init();
mp.clear();
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%s",s[i]);
ac.add(s[i],i);
int x=BKDRHash(s[i]);
mp[x]++;
}
ac.getfail();
scanf("%s",T);
ac.Find(T);
int ans=;
for(int i=;i<=n;i++){
int x=ac.cnt[i];
int y=BKDRHash(s[i]);
if(x>){
ans+=mp[y];
}
}
printf("%d\n",ans);
}
return ;
}

HDU 2222 Keywords Search(瞎搞)的更多相关文章

  1. HDU 2222 Keywords Search(查询关键字)

    HDU 2222 Keywords Search(查询关键字) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...

  2. HDU 2222 Keywords Search(AC自动机模版题)

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

  3. hdu 2222 Keywords Search ac自己主动机

    点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  4. hdu 2222 Keywords Search 模板题

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

  5. hdu 2222 Keywords Search - Aho-Corasick自动机

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  6. hdu 2222 Keywords Search

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路:裸AC自动机,直接贴代码做模板 #include<stdio.h> #includ ...

  7. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

  8. HDU 2222 Keywords Search(AC自动机模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出多个单词,最后再给出一个模式串,求在该模式串中包含了多少个单词. 思路: AC自动机的模板题. ...

  9. 【刷题】HDU 2222 Keywords Search

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

随机推荐

  1. [USACO08DEC]Trick or Treat on the Farm (拓扑排序,DP)

    题目描述 每年万圣节,威斯康星的奶牛们都要打扮一番,出门在农场的N个牛棚里转 悠,来采集糖果.她们每走到一个未曾经过的牛棚,就会采集这个棚里的1颗糖果. 农场不大,所以约翰要想尽法子让奶牛们得到快乐. ...

  2. 【ztree】zTree取消树节点选中的背景色

    点击树节点的时候是ztree给树加了个class:    curSelectedNode 所以最简单的清除树节点的背景色的方法是移除其有背景色的class: $(".curSelectedN ...

  3. Java面试题集(五)

    三.开源框架 什么是mybaties? Mybaties是支持普通sql查询,存储过程和高级映射的优秀持久层框架.Mybaties消除了几乎所有的jdbc代码和参数的手工设置以及结果集的检索.Myba ...

  4. Mac OS X 10.10.5 中 VirtualBox 5.0 里的 Win7 虚拟机无法使用 USB 3.0 设备的解决办法(补充说明)

    上一篇文章中,我说到了如何在Mac OS X 10.10.5 中让 VirtualBox 5.0 里的 Win7 虚拟机使用 USB 3.0.最近碰巧升级 MacBook Pro 为最新的 15 吋 ...

  5. stm32f103定时器

    1)TIM_TimeBaseInitTypeDef 时基初始化结构体,它包括了四个成员函数:TIM_ClockDivision.TIM_CounterMode.TIM_Period.TIM_Presc ...

  6. Java面试题总结之数据结构、算法和计算机基础(刘小牛和丝音的爱情故事1)

      Java面试题总结之数据结构.算法和计算机基础(刘小牛和丝音的爱情故事1)​mp.weixin.qq.com 全文字数: 1703 阅读时间: 大约6 分钟 刘小牛是一名Java程序员,由于天天9 ...

  7. paramiko使用exec_command执行rm -rf删除目录的坑

    paramiko删除目录后的上传操作请参考步骤1.2.3的说明 try: ssh = SSHClient(ip,user) sftpClient = ssh.getSftpClient() outpu ...

  8. tensorflow基础练习:线性模型

    TensorFlow是一个面向数值计算的通用平台,可以方便地训练线性模型.下面采用TensorFlow完成Andrew Ng主讲的Deep Learning课程练习题,提供了整套源码. 线性回归 多元 ...

  9. Bootstrap全局CSS样式之button和图片

    .btn-default--button的默认样式. .btn-primary--button的首选样式: .btn-success--button的成功样式: .btn-info--button的一 ...

  10. 【Java编程】建立一个简单的JDBC连接-Drivers, Connection, Statement and PreparedStatement

    本blog提供了一个简单的通过JDBC驱动建立JDBC连接例程.并分别通过Statement和PreparedStatement实现对数据库的查询. 在下一篇blog中将重点比較Statement与P ...