HDU 2222 ----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.
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.
5
she
he
say
shr
her
yasherhs
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std; struct Trie
{
int next[][],fail[],end[];
int root,L;
int newnode()
{
for(int i=;i<;i++)
{
next[L][i]=-;
}
end[L++]=;
return L-;
}
void init()
{
L=;
root=newnode();
}
void insert(char buf[])
{
int len=strlen(buf);
int now=root;
for(int i=;i<len;i++)
{
if(next[now][buf[i]-'a']==-)
next[now][buf[i]-'a']=newnode();
now=next[now][buf[i]-'a'];
}
end[now]++;
}
void build()
{
queue<int>q;
fail[root]=root;
for(int i=;i<;i++)
{
if(next[root][i]==-) next[root][i]=root;
else
{
fail[next[root][i]]=root;
q.push(next[root][i]);
}
}
while(!q.empty())
{
int now=q.front();
q.pop();
for(int i=;i<;i++)
{
if(next[now][i]==-)
next[now][i]=next[fail[now]][i];
else
{
fail[next[now][i]]=next[fail[now]][i];
q.push(next[now][i]);
}
}
}
}
int query(char buf[])
{
int len=strlen(buf),now=root,res=;
for(int i=;i<len;i++)
{
now=next[now][buf[i]-'a'];
int temp=now;
while(temp!=root)
{
res+=end[temp];
end[temp]=;
temp=fail[temp];
}
}
return res;
}
}; char buf[];
Trie ac;
int main()
{
//freopen("in.txt","r",stdin);
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
ac.init();
for(int i=;i<n;i++)
{
scanf("%s",buf);
ac.insert(buf);
}
ac.build();
scanf("%s",buf);
printf("%d\n",ac.query(buf));
}
return ;
}
HDU 2222 ----AC自动机的更多相关文章
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 2222 (AC自动机)
HDU 2222 Keywords search Problem : 给若干个模式串,询问目标串中出现了多少个模式串. Solution : 复习了一下AC自动机.需要注意AC自动机中的fail,和n ...
- HDU 2222 AC自动机 裸题
题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...
- HDU 2222 AC自动机模版题
所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...
- hdu 2222(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu 2222 ac自动机更新模板 for onSite contest
http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 #include <cstdio> #include <cstdlib> ...
- HDU 2222 AC自动机(模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Keywords Search HDU - 2222 AC自动机板子题
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...
- HDU 2222 & ac自动机模板
题意: 求n个模板串在匹配串中出现了几个. SOL: 反正就是模板啦...似乎比KMP都简单----这么说似乎有点不道德...毕竟先看的KMP而他们并没有什么不同... 貌似自己的理解和他们画的图还是 ...
随机推荐
- JAVA第三周课后作业
JAVA课后作业 一.枚举类型 代码: enum Size{SMALL,MEDIUM,LARGE}; public cl ass EnumTest { public static void main( ...
- (转)深入理解Java中的final关键字
转自:http://www.importnew.com/7553.html Java中的final关键字非常重要,它可以应用于类.方法以及变量.这篇文章中我将带你看看什么是final关键字?将变量,方 ...
- Unity3D Animation Curve
http://docs.unity3d.com/430/Documentation/Manual/UsingAnimCurves.html
- 第一次使用Android Studio时你应该知道的一切配置
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- php 利用ffmpeg将amr转MP3
原文地址: http://www.jianshu.com/p/895d5568ce70 http://www.cnblogs.com/wanghetao/p/3386311.html http://w ...
- [转] Memcached参数设置 命令
只是看到了..这些命令肯定是没问题的.. 笔者现在也还不会用..汗..先留着..找到怎么用了在回来修改.. memcached的基本设置: -p 监听的端口-l 连接的IP地址, 默认是本机-d st ...
- Linux查看软件安装路径
Linux中查看某 个软件的安装路径(地址)有时显得非常重要.比如某个文件的快速启动项被删除,或者你要建立快速启动项,或者想删除. 添加安装文件等等,很多地方都要用到查案文件安装路径的命令. 这里给大 ...
- html css 网络 页面布局 颜色 参考 拾取器网址
http://blog.163.com/wujinhongisme@126/blog/static/3613698020095115919389/ ========================== ...
- 阿里云9折推荐码:0LGVW2
阿里云9折推荐码:0LGVW2,第一次购买云服务器或云数据库可享受原价9折优惠.
- 2015年11月25 Java基础系列(二)Thread Runnable线程初级讲解
序,线程是比进程小的进程,非常广泛的被使用. 一.继承Thread实现线程操作 1.注意setDaemon(boolean)方法,参数为true时为守护线程,参数为false时为用户线程. 守护线程的 ...