HDU2222 Keywords Search ac自动机第一题
指针我一般都会出错,所以还是自己写数组版本。
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.
InputFirst 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.
OutputPrint how many keywords are contained in the description.Sample Input
1
5
she
he
say
shr
her
yasherhs
Sample Output
3
第一版:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int maxn=;
int Next[maxn][],End[maxn],fail[maxn];
int cnt,root=,ans;//root不能为0
void _init()
{
memset(Next,,sizeof(Next));
memset(End,,sizeof(End));
memset(fail,,sizeof(fail));
ans=;cnt=;
}
void _insert(char s[])
{
int L=strlen(s);
int now=root;
for(int i=;i<L;i++){
if(!Next[now][s[i]-'a']) Next[now][s[i]-'a']=++cnt;
now=Next[now][s[i]-'a'];
}
End[now]++;
}
void _build()//bfs
{
queue<int>q;
q.push(root);
while(!q.empty()){
int Now=q.front();q.pop();
for(int i=;i<;i++){
if(Next[Now][i]){
if(Now==root) fail[Next[Now][i]]=root;
else{
int p=fail[Now];
while(p){//给儿子们找对象
if(Next[p][i]){
fail[Next[Now][i]]=Next[p][i];
break;//找到了就停止
}
p=fail[p];
}
if(!p) fail[Next[Now][i]]=root;//不晓得?
}
q.push(Next[Now][i]);
}
}
}
}
void _query(char s[])
{
int L=strlen(s);
int Now=root;
for(int i=;i<L;i++){
int x=s[i]-'a';
while(Now!=root&&!Next[Now][x]) Now=fail[Now];
Now=Next[Now][x];
if(!Now) Now=root;//不晓得?
int tmp=Now;
while(tmp!=root){
if(End[tmp]>=){
ans+=End[tmp];
End[tmp]=-;//避免重复
}
else break;
tmp=fail[tmp];
}
}
}
int main()
{
char s[];
char c[maxn] ;
int n,j,i,T;
scanf("%d",&T);
while(T--){ _init();
scanf("%d",&n);
for(i=;i<=n;i++){
scanf("%s",s);
_insert(s);//单词
}
scanf("%s",c); _build();
_query(c);//文章 printf("%d\n",ans); }
return ;
}
稍微改进版:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int maxn=;
int Next[maxn][],End[maxn],fail[maxn];
int cnt,root=,ans;
int q[],tail,head;
void _init()
{
//不要memset只要200ms
ans=;cnt=;fail[root]=-;//root的fail不能等于本身,不然不能重新开始。
head=tail=;End[root]=;
for(int i=;i<;i++) Next[root][i]=;
}
void _insert(char s[])
{
int now=root;
for(int i=;s[i];i++){
if(!Next[now][s[i]-'a']) {
Next[now][s[i]-'a']=++cnt;
fail[cnt]=;
End[cnt]=;
for(int i=;i<;i++) Next[cnt][i]=;
}
now=Next[now][s[i]-'a'];
}
End[now]++;
}
void _build()//bfs
{
q[++head]=root;
while(tail<head){
int Now=q[++tail];
for(int i=;i<;i++){
if(Next[Now][i]){
if(Now==root) fail[Next[Now][i]]=root;
else{
int p=fail[Now];
while(p!=-){//给儿子们找对象
if(Next[p][i]){
fail[Next[Now][i]]=Next[p][i];
break;//找到了就停止
}
p=fail[p];//配对
}
if(p==-) {
fail[Next[Now][i]]=root;//重新开始
}
}
q[++head]=Next[Now][i];
}
}
}
}
void _query(char s[])
{
int L=strlen(s);
int Now=root;
for(int i=;i<L;i++){
int x=s[i]-'a';
while(Now!=root&&!Next[Now][x]) Now=fail[Now];
Now=Next[Now][x];
if(Now==-) Now=root;//即使失败,也不气馁,一切从零开始
int tmp=Now;
while(tmp!=root){
if(End[tmp]>){
ans+=End[tmp];
End[tmp]=-;//避免重复
}
else break;
tmp=fail[tmp];
}
}
}
int main()
{
char s[];
char c[maxn] ;
int n,j,i,T;
scanf("%d",&T);
while(T--){ _init();
scanf("%d",&n);
for(i=;i<=n;i++){
scanf("%s",s);
_insert(s);//单词
}
scanf("%s",c); _build();
_query(c);//文章 printf("%d\n",ans); }
return ;
}
HDU2222 Keywords Search ac自动机第一题的更多相关文章
- hdu2222 KeyWords Search AC自动机入门题
/** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...
- hdu2222 Keywords Search ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...
- HDU2222 Keywords Search —— AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- hdu2222 Keywords Search (AC自动机板子
https://vjudge.net/problem/HDU-2222 题意:给几个模式串和一个文本串,问文本串中包含几个模式串. 思路:贴个板子不解释. #include<cstdio> ...
- 【HDU 2222】Keywords Search AC自动机模板题
参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...
- HD2222 Keywords Search(AC自动机入门题)
然而还不是很懂=_= #include <iostream> #include <cstring> #include <algorithm> #include &l ...
随机推荐
- detectron——test 错误集锦
一.测试错误,运行如下代码 python2 tools/test_net.py --cfg experiments/e2e_faster_rcnn_resnet--FPN_pascal2007.yam ...
- 用 SqlConnectionStringBuilder 来写连接字符串,向连接字符串添加设置
正常情况下写的连接字符串: connStr = "Data Source=127.0.0.1;DataBase=Hydor;UID=***;PWD=***;Pooling=true;Min ...
- Linux计划任务,自动删除n天前的旧文件
Linux计划任务,自动删除n天前的旧文件 linux是一个很能自动产生文件的系统,日志.邮件.备份等.虽然现在硬盘廉价,我们可以有很多硬盘空间供这些文件浪费,但需求总是多方面的嘛-我就觉得让系统定时 ...
- php入门(三)
PHP 面向对象: 在php5中 var就是public的别名. 变量 $this 代表自身的对象. PHP_EOL;为换行符 构造函数+析构函数 <?php class Site { /* 成 ...
- jmeter-time函数
别人写的一个详解置顶 http://www.cnblogs.com/MasterMonkInTemple/p/3442770.html 新建beanshell,time函数格式${_time(YYYY ...
- LeetCode第[78]题(Java):Subsets(求子集)扩展——第[90]题:Subsets 2
题目:矩阵置0 难度:Easy 题目内容: Given a set of distinct integers, nums, return all possible subsets (the pow ...
- Entity Framework教程及文章传送门
Entity Framework视频教程http://www.iqiyi.com/playlist296844502.html Model-Code First做法講解與教學 (ASP.net MVC ...
- 淘汰算法 LRU、LFU和FIFO
含义: FIFO:First In First Out,先进先出LRU:Least Recently Used,最近最少使用 LFU:Least Frequently Used,最不经常使用 以上三者 ...
- spring mvc: 生成RSS源
spring mvc: 生成RSS源 准备: 从相同的maven存储库页面下载 Rome 库及其依赖项rome-utils,jdom和slf4j.和所需的依赖关系 <!-- rss源依赖 --& ...
- Codeforces Round #409
第一题很水但是wa了一发,因为没考虑K前面是K的情况 #include<map> #include<set> #include<cmath> #include< ...