Keywords Search

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

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:  3065 2243 2825 3341 3247 
 

题意:

给定n个模式串和一个文本串,统计文本串中模式串的个数。

思路:

AC自动机模板。

多组数据一定要注意初始化!

 #include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
//#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f int t, n;
const int maxn = 5e5 + ;
const int maxlen = 1e6 + ; struct tree{
int fail;
int son[];
int ed;
}AC[maxlen];
int tot = ;
char s[maxlen]; void build(char s[])
{
int len = strlen(s);
int now = ;
for(int i = ; i < len; i++){
if(AC[now].son[s[i] - 'a'] == ){
AC[now].son[s[i] - 'a'] = ++tot;
}
now = AC[now].son[s[i] - 'a'];
}
AC[now].ed += ;
} void get_fail()
{
queue<int>que;
for(int i = ; i < ; i++){
if(AC[].son[i] != ){
AC[AC[].son[i]].fail = ;
que.push(AC[].son[i]);
}
}
while(!que.empty()){
int u = que.front();
que.pop();
for(int i = ; i < ; i++){
if(AC[u].son[i] != ){
AC[AC[u].son[i]].fail = AC[AC[u].fail].son[i];
que.push(AC[u].son[i]);
}
else{
AC[u].son[i] = AC[AC[u].fail].son[i];
}
}
}
} int AC_query(char s[])
{
int len = strlen(s);
int now = , ans = ;
for(int i = ; i < len; i++){
now = AC[now].son[s[i] - 'a'];
for(int t = now; t && AC[t].ed != -; t = AC[t].fail){
ans += AC[t].ed;
AC[t].ed = -;
}
}
return ans;
} int main()
{
scanf("%d", &t);
while(t--){
for(int i = ; i <= tot; i++){
AC[i].ed = ;
AC[i].fail = ;
for(int j = ; j < ; j++){
AC[i].son[j] = ;
}
}
tot = ;
scanf("%d", &n);
for(int i = ; i < n; i++){
scanf("%s", s);
build(s);
}
AC[].fail = ;
get_fail();
scanf("%s", s);
printf("%d\n", AC_query(s));
}
return ;
}

hdu2222 Keywords Search【AC自动机】的更多相关文章

  1. hdu2222 Keywords Search ac自动机

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...

  2. HDU2222 Keywords Search [AC自动机模板]

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

  3. HDU2222 Keywords Search —— AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...

  4. hdu2222 KeyWords Search AC自动机入门题

    /** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...

  5. HDU2222 Keywords Search ac自动机第一题

    指针我一般都会出错,所以还是自己写数组版本. In the modern time, Search engine came into the life of everybody like Google ...

  6. hdu2222 Keywords Search (AC自动机板子

    https://vjudge.net/problem/HDU-2222 题意:给几个模式串和一个文本串,问文本串中包含几个模式串. 思路:贴个板子不解释. #include<cstdio> ...

  7. 【HDU2222】Keywords Search AC自动机

    [HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  8. [hdu2222] [AC自动机模板] Keywords Search [AC自动机]

    AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...

  9. Keywords Search(AC自动机模板)

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

  10. Keywords Search AC自动机

    In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...

随机推荐

  1. 转:Hibernate query.list()之卡住问题

    某个函数里面有调用Query的list()方法,然后它有时会出现这种症状: 忽然停住不动,但是也没报异常,就是界面死了. 我的查询差不多是这样: Query q=sessionFactory.open ...

  2. spring boot工程打成JAR包到服务器上运行

    只需在项目的pom.xml中加入下面插件 <build> <plugins> <plugin> <groupId>org.springframework ...

  3. jQuery获取CSS样式中的颜色值的问题

    转自:http://blog.csdn.net/cwj649956781/article/details/23261529 jQuery获取CSS样式中的颜色值的问题,不同浏览器格式不同的解决办法,需 ...

  4. Windows程序执行过程

    Windows应用程序: WinMain函数(入口函数): 1. 设计窗体类,注冊窗体类.WNDCLASS 2. 创建窗体,显示及更新窗体. 3. 消息循环. 窗体过程函数(回调函数):WindowP ...

  5. JAVAWEB开发之Session的追踪创建和销毁、JSP具体解释(指令,标签,内置对象,动作即转发和包括)、JavaBean及内省技术以及EL表达式获取内容的使用

    Session的追踪技术 已知Session是利用cookie机制的server端技术.当client第一次訪问资源时 假设调用request.getSession() 就会在server端创建一个由 ...

  6. SpringMVC -- 梗概--源码--贰--静态资源的访问问题

    配置:<mvc:default-servlet-handler/> 1>静态资源:除了Servlet.Controller之外的资源,如:js,css,png,html等 2> ...

  7. 10 -- 深入使用Spring -- 5...2 在Spring中使用Quartz

    10.5.2 在Spring中使用Quartz Spring 的任务调度抽象层简化了任务调度,在Quartz基础上提供了更好的调度抽象.本系统使用Quartz框架来完成任务调度,创建Quartz的作业 ...

  8. oracle nvl,having的用法

    select oi.order_id,opl.payment_no,opl.back_no, oi.commit_time, oi.receive_mobile, oi.receive_user, n ...

  9. mosquitto 参数配置

    mosquitto 参数配置 1.retry_interval  当QoS为1或2的消息已经被发送后,mosquitto在一段时间内仍未接收到客户端的反馈消息,将重新发送消息. 默认为20秒 2.sy ...

  10. Spring系列之IOC容器

    一.概述 IOC容器就是具有依赖注入功能的容器,IOC容器负责实例化.定位.配置应用程序中的对象及建立这些对象之间的依赖.应用程序无需直接在代码中new 相关的对象,应用程序由IOC容器进行组装.在S ...