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.

--by HDU;

http://acm.hdu.edu.cn/showproblem.php?pid=2222



  给一份Key_word和一串字符,查询有多少K_w在字串中;

  唉,你看这题,你看她,她又是一道萝莉裸题,你不知道,裸题不能抄题解吗?

  要自己打知道吗?

  。。。。

  本题显然是AC自动机,然而我却写成了TLE自动机(话说哪有数组开小就死循环的。。)

先建一棵trie

  代码:

    for(i=;i<=n;i++){
getchar();
scanf("%s",key[i]);
k=;
len=strlen(key[i])-;
for(j=;j<=len;j++){
if(!data[k].ch[key[i][j]-'a'])
data[k].ch[key[i][j]-'a']=++tot;
k=data[k].ch[key[i][j]-'a'];
}
is_end[k]++;
}

(有关trie的,POJ P2001)

  然后连上fail;

  bfs顺序求fail保证先求父节点i再求子节点j,fail[j]=fail[i].ch(子节点的fail为父节点的fail的子节点或父节点的fai的faill的子节点或...);

  代码:

void bfs_fail()
{
memset(vis,,sizeof(vis));
int i,j;
que[]=;h=;t=;
while(h<t){
++h;
for(i=;i<=;i++)
if(data[que[h]].ch[i]){
j=que[h];
while(){
if(data[j].ch[i]&&data[j].ch[i]!=data[que[h]].ch[i]){
fail[data[que[h]].ch[i]]=data[j].ch[i]; break;}
else{
if(!j)
break;
j=fail[j];
}
}
t++;
if(!vis[data[que[h]].ch[i]]){
que[t]=data[que[h]].ch[i];
vis[que[t]]=;
}
}
}
}

这样自动机就建好了!!

然后是匹配:

代码:

//给我自动跑!!

请无视上行;

void match()
{
int tem1=,tem2=,len;
len=strlen(des);
while(tem2!=len){
if(data[tem1].ch[des[tem2]-'a']){
tem1=data[tem1].ch[des[tem2]-'a'];
tem2++;
find(tem1);
}
else{
if(tem1==)
tem2++;
tem1=fail[tem1];
}
}
}

你看到一个find(tem1)这是什么?

她的目的是在跑AC自动机时查询必须查询的值——查询未被查询的is_end标记,因为当我们查询到一个匹配的的点时,她的fail链上的is_end要被加入ans中,因为这些is_end所代表的key_word是你当匹配过的字符集(串?序列?)的后缀。

总代码如下:

 #include<cstdio>
#include<cstring>
using namespace std;
int n,ans;
char key[][];
struct ss{
int ch[];
}data[];
int tot;
int is_end[];
int fail[];
int que[],h,t;
char des[];
int vis[];
void bfs_fail();
void work();
void match();
void find(int );
int main()
{
int T;
scanf("%d",&T);
while(T--)
work();
return ;
}
void work()
{
int i,j,k,len;
memset(data,,sizeof(data));
memset(is_end,,sizeof(is_end));
memset(fail,,sizeof(fail));
scanf("%d",&n);
for(i=;i<=n;i++){
getchar();
scanf("%s",key[i]);
k=;
len=strlen(key[i])-;
for(j=;j<=len;j++){
if(!data[k].ch[key[i][j]-'a'])
data[k].ch[key[i][j]-'a']=++tot;
k=data[k].ch[key[i][j]-'a'];
}
is_end[k]++;
}
bfs_fail();
getchar();
scanf("%s",des);
ans=;match();
printf("%d\n",ans);
}
void bfs_fail()
{
memset(vis,,sizeof(vis));
int i,j;
que[]=;h=;t=;
while(h<t){
++h;
for(i=;i<=;i++)
if(data[que[h]].ch[i]){
j=que[h];
while(){
if(data[j].ch[i]&&data[j].ch[i]!=data[que[h]].ch[i]){
fail[data[que[h]].ch[i]]=data[j].ch[i]; break;}
else{
if(!j)
break;
j=fail[j];
}
}
t++;
if(!vis[data[que[h]].ch[i]]){
que[t]=data[que[h]].ch[i];
vis[que[t]]=;
}
}
}
}
void match()
{
int tem1=,tem2=,len;
len=strlen(des);
while(tem2!=len){
if(data[tem1].ch[des[tem2]-'a']){
tem1=data[tem1].ch[des[tem2]-'a'];
tem2++;
find(tem1);
}
else{
if(tem1==)
tem2++;
tem1=fail[tem1];
}
}
}
void find(int tem)
{
int i;
while(tem){
if(is_end[tem]){
ans+=is_end[tem];
is_end[tem]=;
}
tem=fail[tem];
}
}

祝AC哟

HDU P2222 Keywords Search的更多相关文章

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

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

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

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

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

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

  4. HDU 2222 Keywords Search(瞎搞)

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

  5. hdu 2222 Keywords Search 模板题

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

  6. hdu 2222 Keywords Search - Aho-Corasick自动机

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  7. 【刷题】HDU 2222 Keywords Search

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

  8. hdu 2222 Keywords Search

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路:裸AC自动机,直接贴代码做模板 #include<stdio.h> #includ ...

  9. HDU 2222 Keywords Search (AC自动机)

    题意:给你一些模式串,再给你一串匹配串,问你在匹配串中出现了多少种模式串,模式串可以相同 AC自动机:trie树上进行KMP.首先模式串建立trie树,再求得失配指针(类似next数组),其作用就是在 ...

随机推荐

  1. 分享一些JAVA常用的学习网站

    常用学习网站freecodecamp 一个非常好的网站,教学模式类似游戏中的闯关,通过每关之后会有成就感,在该网站还有设有聊天室,可以进行相关的技术交流,很棒的学习网站. https://www.fr ...

  2. Eureka客户端注册过程源码解析

    微服务中注册中心是其重要的组成部分,那么客户端是如何注册到注册中心的呢,下面我们进入源码查看. 客户端的注册标志是@EnableDiscoveryClient,我们点进入注解查看 注解介绍这是开启Di ...

  3. 队列的理解和实现(二) ----- 链队列(java实现)

    什么是链队列 链队是指采用链式存储结构实现的队列,通常链队用单链表俩表示.一个链队显然需要两个分别指示队头和队尾的指针,也称为头指针和尾指针,有了这两个指针才能唯一的确定. package 链队列; ...

  4. FlowPortal-BPM——数据库交互:创建新接口(类库)—将数据提交给其他程序使用

    使用到的是“流程设计”→“自定义插件” 一.创建新类库 (1)新建类库→引用文件 (2)新建ado.net数据访问类(要操作的数据库) (3)右键类库名称→属性→生成→输出→路径:安装目录下UserD ...

  5. canvas+js+面向对象的圆形封装

    效果: Circle.js /* 1. 封装属性: x, y r, fillStyle strokeStyle opacity 2.render */ function Circle(option) ...

  6. OpenERP 中的on_change方法总结

    1.xml中应为on_change=""的形式 2.py文件中 self,cr,uid,ids为必备参数,后面的参数根据xml文件中的参数的数量而定 3.return的是一个字典, ...

  7. spring boot快速入门 1 :创建项目、 三种启动项目方式

    准备工作: (转载)IDEA新建项目时,没有Spring Initializr选项 最近开始使用IDEA作为开发工具,然后也是打算开始学习使用spring boot. 看着博客来进行操作上手sprin ...

  8. Dalvik与JVM区别

    1.Dalvik出现和SDK层面采用java为开发语言的原因 1.1 避免Native作为应用代码导致的因为设备多样化导致App生态了支离破碎,是从Nokia哪里的教训. 1.2 重新实现Dalvik ...

  9. Javac之Environment

    关于Env的源代码如下: /** A class for environments, instances of which are passed as * arguments to tree visi ...

  10. @Scheduled执行定时任务与cron表达式

    1 配置文件形式执行定时任务 1 1.X 版本与spring结合使用实例 1.1 常用maven管理 pom.xml文件 <project xmlns="http://maven.ap ...