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. 算法复习——单调队列(sliding windows,ssoi)

    题目: 题目描述 给你一个长度为 N 的数组,一个长为 K 的滑动的窗体从最左移至最右端,你只能见到窗口的 K 个整数,每次窗体向右移动一位,如下表:

  2. Merge into语句用法及其效率问题

    Merge into语句用法及其效率问题 /*Merge into 详细介绍MERGE语句用来合并UPDATE和INSERT语句.通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查询, ...

  3. JSONObject与JSONArray的使用(jackson)

    1.创建一个JSONObject对象 package com.resource.controller.web; import java.util.ArrayList; import java.util ...

  4. ElasticSearch分词器

    什么是分词器? 分词器,是将用户输入的一段文本,分析成符合逻辑的一种工具.到目前为止呢,分词器没有办法做到完全的符合人们的要求.和我们有关的分词器有英文的和中文的.英文的分词器过程:输入文本-关键词切 ...

  5. PHP的Trait机制

    PHP的Trait机制 Trait介绍: 1.自PHP5.4起,PHP实现了一种代码复用的方法,称为trait.2.Trait是为类似PHP的单继承语言二准备的一种代码复用机制.3.Trait为了减少 ...

  6. Laravel 之Cache缓存

    写入缓存 Cache::put('key','value',10);//设置10分钟 获取缓存 Cache::get('key'); 增加缓存 Cache::add('key','value',10) ...

  7. CKeditor如何实现图片上传功能

    http://makaiyuan.blog.51cto.com/5819595/1049521 如何在数据库中导入excel文件内的数据:http://jingyan.baidu.com/album/ ...

  8. VM虚拟机

    VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转换模式)和host-only(主机模式).要想在网络管理和维护中合理应用它们,你就应该先了解一下这三种工作模式. 1 ...

  9. Camtasia Studio录制屏幕字迹不清晰的原因

    Camtasia Studio这是一个很优秀的屏幕录像软件,功能强大且录制效果出色,支持众多格式输出: 之前一直用它录制视频的都很正常,但这次换系统后再重新安装后录制视频时,发现输出的视频画质不佳,文 ...

  10. jquery ajax 跨域訪问样例

    <script type="text/javascript"> $(function(){    $.ajax({    cache : false,    type ...