hdoj 2222 Keywords Search 【AC自己主动机 入门题】 【求目标串中出现了几个模式串】
Keywords Search
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.
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.
1
5
she
he
say
shr
her
yasherhs
3
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#define MAXN 500000+10
using namespace std;
char str[1000000+10];
struct Trie
{
int next[MAXN][30], fail[MAXN], word[MAXN];
int root, L;
int newnode()//新建节点
{
for(int i = 0; i < 26; i++)
next[L][i] = -1;//初始化-1
word[L++] = 0;
return L-1;
}
void init()//初始化节点
{
L = 0;
root = newnode();
}
//插入字符串 同字典树 比較好理解
void Insert(char *buf)
{
int len = strlen(buf);
int now = root;
for(int i = 0; i < len; i++)
{
if(next[now][buf[i]-'a'] == -1)
next[now][buf[i]-'a'] = newnode();//新建
now = next[now][buf[i]-'a'];
}
word[now]++;//数目加一
}
//构造失败指针
void Build()
{
queue<int> Q;
fail[root] = root;
for(int i = 0; i < 26; i++)
{
if(next[root][i] == -1)
next[root][i] = root;//指向root
else
{
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
}
while(!Q.empty())
{
int now = Q.front();
Q.pop();
for(int i = 0; i < 26; i++)
{
if(next[now][i] == -1)
next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]]=next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
}
//查询 相似KMP
int Query(char *buf)
{
int len = strlen(buf);
int now = root;
int ans = 0;//查询结果
for(int i = 0; i < len; i++)
{
now = next[now][buf[i]-'a'];
int temp = now;//记录now 从当前開始匹配
while(temp != root)//直到指向root 为止
{
ans += word[temp];
word[temp] = 0;
temp = fail[temp];
}
}
return ans;
}
};
Trie ac;
int main()
{
int t, N;
scanf("%d", &t);
while(t--)
{
scanf("%d", &N);
ac.init();
for(int i = 0; i < N; i++)
{
scanf("%s", str);
ac.Insert(str);
}
ac.Build();
scanf("%s", str);
printf("%d\n", ac.Query(str));
}
return 0;
}
hdoj 2222 Keywords Search 【AC自己主动机 入门题】 【求目标串中出现了几个模式串】的更多相关文章
- HDU 2222 Keywords Search AC自己主动机入门题
单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- hdoj 2222 Keywords Search(AC自动机)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路分析:该问题为多模式匹配问题,使用AC自动机解决:需要注意的问题是如何统计该待查询的字符串包 ...
- AC自动机 HDOJ 2222 Keywords Search
题目链接 题意:每个文本串的出现次数 分析:入门题,注意重复的关键字算不同的关键字,还有之前加过的清零. 新模板,加上last跑快一倍 #include <bits/stdc++.h> ...
- HDU 2222 Keywords Search (AC自动机)
题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...
- NYOJ 1085 数单词 (AC自己主动机模板题)
数单词 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 为了可以顺利通过英语四六级考试,如今大家每天早上都会早起读英语. LYH本来以为自己在6月份的考试中能够通过六 ...
- hdu5384 AC自己主动机模板题,统计模式串在给定串中出现的个数
http://acm.hdu.edu.cn/showproblem.php?pid=5384 Problem Description Danganronpa is a video game franc ...
- HDU 2222 Keywords Search(AC自己主动机模板题)
题意:给出一个字符串和若干个模板,求出在文本串中出现的模板个数. 思路:由于有可能有反复的模板,trie树权值记录每一个模板出现的次数就可以. #include<cstdio> #incl ...
- hdu2222--Keywords Search+AC自己主动机模板
题目链接:pid=2222">点击进入 KMP对模式串进行处理.然后就能够方便的推断模式串是否在目标串中出现了:这显示适合一个模式串多个目标串的情况.可是假设模式串有多个,这时假设还用 ...
随机推荐
- c# 与java base64 不一致解决方案
不一致的问题不是编码的问题 而是json字符串的问题通常我们会json 嵌套 我们先来看连个字符串 {"contentType":"","http ...
- WCF:目录
ylbtech-WCF:目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://ylbtech.c ...
- TCP/IP协议族简介
OSI网络分层介绍 网络结构的标准模型是OSI模型,由国际互联网标准化组织定义的网络分层模型.虽然目前没有完全按照这种模型实现的网络协议栈,但是学习这个模型对于我们理解网络协议还是很有帮助的. 1.O ...
- BZOJ 1500 splay终结版...
GSS系列有一丝丝像- 只不过那个是线段树 这个是splay 翻转 插入 删除啥的就是普通的splay 合在一起了而已 //By SiriusRen #include <cstdio> # ...
- linux 下vim中关于删除某段,某行,或者全部删除的命令
- Java并发--线程安全策略
1 不可变对象 用不可变对象保证线程安全,是相当于不让线程并发,逃避了并发. 不可变对象就是指一个类的实例化对象不可变.比如String类的实例 主要方法有: 将类声明为final 将所有成员声明为 ...
- 路飞学城Python-Day49
55-善于使用父亲的padding,而不是margin 56-文本属性和字体属性 div{ width: 300px; height: 100px; border: 1px solid red; /* ...
- 路飞学城Python-Day40(第四模块复习题)
数据库 一.简答题 1.说说你所知道的MySQL数据库存储引擎,InnoDB存储引擎和MyISM存储引擎的区别? 1.InnoDB存储引擎(MySQL默认存储引擎),支持事务,其设计目标主要面向联机事 ...
- NYOJ 737 石子合并(一)
题意 排成一排的石子,每次合并相邻两堆并由一定的代价,求合并成一堆的最小代价 解法 区间dp 枚举长度 dp[i,j]表示合并石子堆编号从i到j为一堆所需的最小代价(这个题目的代价是sum(i..j) ...
- redis 篇 - hash
hash 可以认为是 python 中的字典 field 不允许重复 string类型的field和value的映射表 每个hash可以存储 232 - 1 键值对(40多亿) 方法 hest key ...