Keywords Search

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

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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  2896 3065 2243 2825 3341 

分析:

AC自动机模板题...

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
using namespace std; const int maxn=1000000+5,maxm=50+5; int n,cas,tot,head,tail,q[500000+5]; char s[maxn],word[maxm]; struct trie{
int cnt,fail,nxt[26];
}tr[500000+5]; inline void init(void){
for(int i=0;i<=tot;tr[i].fail=-1,tr[i].cnt=0,i++)
for(int j=0;j<26;j++)
tr[i].nxt[j]=0;
tot=0;
} inline void insert(char *word){
int p=0,len=strlen(word);
for(int i=0;i<len;i++){
if(!tr[p].nxt[word[i]-'a'])
tr[p].nxt[word[i]-'a']=++tot;
p=tr[p].nxt[word[i]-'a'];
}
tr[p].cnt++;
} inline void buildACM(void){
head=0,tail=0;q[0]=0;
while(head<=tail){
int id=q[head++],p=-1;
for(int i=0;i<26;i++)
if(tr[id].nxt[i]){
if(id){
p=tr[id].fail;
while(p!=-1){
if(tr[p].nxt[i]){
tr[tr[id].nxt[i]].fail=tr[p].nxt[i];
break;
}
p=tr[p].fail;
}
if(p==-1) tr[tr[id].nxt[i]].fail=0;
}
else
tr[tr[id].nxt[i]].fail=0;
q[++tail]=tr[id].nxt[i];
}
}
} inline int query(void){
int i=0,ans=0,index,len=strlen(s),p=0;
while(s[i]){
index=s[i]-'a';
while(!tr[p].nxt[index]&&p) p=tr[p].fail;
p=tr[p].nxt[index];
int tmp=p;
while(tmp&&tr[tmp].cnt!=-1)
ans+=tr[tmp].cnt,tr[tmp].cnt=-1,tmp=tr[tmp].fail;
i++;
}
return ans;
} signed main(void){
scanf("%d",&cas);
while(cas--){
tot=500000;init();
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%s",word),insert(word);
buildACM();
scanf("%s",s);
printf("%d\n",query());
}
return 0;
}

  


By NeighThorn

HDOJ 2222: Keywords Search的更多相关文章

  1. AC自动机 HDOJ 2222 Keywords Search

    题目链接 题意:每个文本串的出现次数 分析:入门题,注意重复的关键字算不同的关键字,还有之前加过的清零.   新模板,加上last跑快一倍 #include <bits/stdc++.h> ...

  2. hdoj 2222 Keywords Search 【AC自己主动机 入门题】 【求目标串中出现了几个模式串】

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

  3. hdoj 2222 Keywords Search(AC自动机)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路分析:该问题为多模式匹配问题,使用AC自动机解决:需要注意的问题是如何统计该待查询的字符串包 ...

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

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

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

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

  6. HDU 2222 Keywords Search(瞎搞)

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

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

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

  8. hdu 2222 Keywords Search 模板题

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

  9. 【HDOJ】2222 Keywords Search

    AC自动机基础题. #include <iostream> #include <cstdio> #include <cstring> #include <cs ...

随机推荐

  1. 【C++学习笔记】强大的算法——spfa

    spfa的定义 PFA算法的全称是:Shortest Path Faster Algorithm,用于求单源最短路,由西南交通大学段凡丁于1994年发表.当给定的图存在负边时,Dijkstra算法就无 ...

  2. PAT 乙级 1078 / 1084

    题目 PAT 乙级 1078 PAT 乙级 1084 题解 1078和1084这两道题放在一块写,主要是因为这两道题的解法和做题思路非常相似:之前我做这一类题没有一个固定的套路,想到哪写到哪,在某种程 ...

  3. [JZOJ] 5935. 小凯学数学

    由Noip2018初赛的知识得,a|b + a&b = a+b 设计一个区间dp,设\(f[l][r][x]\)表示区间\([l,r]\)能否构成\(x\),数据不大,转移暴力枚举 复杂度\( ...

  4. SpringVC 拦截器+自定义注解 实现权限拦截

    1.springmvc配置文件中配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns= ...

  5. 【JAVA】cxf使用springboot与xml配置的差别所导致的问题。

    使用xml时使用以下配置使报文没有加上命名空间时也能正常访问接口.bean定义的前后顺序不影响程序正常注册对象. <!-- 通过Spring创建数据绑定的类 --> <bean id ...

  6. 【css】如何实现响应式布局

    “自适应网页设计”到底是怎么做到的?其实并不难. 首先,在网页代码的头部,加入一行viewport元标签. <meta name="viewport" content=&qu ...

  7. PHP 作用域

  8. 19.VUE学习之- v-for与computed结合功能 筛选实例讲解

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. c++ string vector类

    //string对象的初始化 #include <iostream> #include <string> //typedef std::basic_string<char ...

  10. Girls and Boys-hdu 1068

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...