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. Tomcat 调优技巧

    Tomcat 调优技巧:1.Tomcat自身调优: ①采用动静分离节约Tomcat的性能: ②调整Tomcat的线程池: ③调整Tomcat的连接器: ④修改Tomcat的运行模式: ⑤禁用AJP连接 ...

  2. cf660E Different Subsets For All Tuples

    For a sequence a of n integers between 1 and m, inclusive, denote f(a) as the number of distinct sub ...

  3. 查看Linux每个进程的流量和带宽

    原文:https://blog.csdn.net/monkeynote/article/details/45867803 作为一个系统管理员,有时候需要搞清楚一台机器上的哪个进程占用了较高的网络带宽. ...

  4. 在App_Data中创建数据库获取连接串简便方法!

    原文发布时间为:2008-07-25 -- 来源于本人的百度文章 [由搬家工具导入] 1、在App_Data右击添加一个SQL数据库2、双击该数据库,在左边添加表,并显示表数据进行添加数据3、把刚刚创 ...

  5. Error处理: “非法字符: \65279”的解决办法

    将eclipse项目转为maven项目的时候,编译时遇到 “非法字符: \65279”的报错. 出错内容是: *.java:1: 非法字符: \65279    [javac] package com ...

  6. linux内核中预留4M以上大内存的方法

    在内核中, kmalloc能够分配的最大连续内存为2的(MAX_ORDER-1)次方个page(参见alloc_pages函数,     "if (unlikely(order >= ...

  7. iOS 取应用版本

    
// 应用网址 返回字典中有多种数据 NSString *urlString2 = [NSString stringWithFormat: @"%@", @"http: ...

  8. 嵌入式cpu架构

    原文:http://www.kaixin001.com/repaste/11007221_7220618944.html### 内容: 目前主要CPU架构有ARM.X86/Atom.MIPS.Powe ...

  9. java集合系列之HashMap源码

    java集合系列之HashMap源码 HashMap的源码可真不好消化!!! 首先简单介绍一下HashMap集合的特点.HashMap存放键值对,键值对封装在Node(代码如下,比较简单,不再介绍)节 ...

  10. BUPT复试专题—密码(2009)

    题目描述   输入 有多组输入,每组: 第一行:由26个小写字母组成的串以空格隔开,如 b a c e u f g h i j k l m n o p q r s t v w x y z d v y ...