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. XCode7无证书真机调试教程

    转载自http://altair21.com/156.html 前提条件: XCode版本>=7 1. 进入xcode,菜单栏选择xcode –> preferences (快捷键 com ...

  2. Springboot接口简单实现生成MySQL插入语句

    Springboot接口简单实现调用接口生成MySQL插入语句 在实际测试中,有这样一个需求场景,比如:在性能压力测试中,可能需要我们事先插入数据库中一些相关联的数据. 我们在实际测试中,遇到问题,需 ...

  3. Linux 线程调度策略与线程优先级

    Linux内核的三种调度策略 SCHED_OTHER 分时调度策略. 它是默认的线程分时调度策略,所有的线程的优先级别都是0,线程的调度是通过分时来完成的.简单地说,如果系统使用这种调度策略,程序将无 ...

  4. c语言中变量/函数命名以单下划线(_)和双下划线(__) 开头的意义

    以单下划线(_)表明是标准库的变量 双下划线(__) 开头表明是编译器的变量 建议自己在命名的时候不要用下划线开头,避免与标准库中的命名冲突 命名方法有好多,何必为自己找不自在呢.

  5. F6&F7adjust the volume

    Windows Registry Editor Version 5.00[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard La ...

  6. Hexo博客系列(三)-将Hexo v3.x个人博客发布到GitLab Pages

    [原文链接]:https://www.tecchen.xyz/blog-hexo-env-03.html 我的个人博客:https://www.tecchen.xyz,博文同步发布到博客园. 由于精力 ...

  7. Struts2 漏洞系列之S2-001分析

    0x00 前言   最近在学习java的相关漏洞,所以Struts2的漏洞自然是绕不开的.为了更好的理解漏洞原理,计划把Struts2所有的漏洞自己都做一个复现.并且自己去实现相关的POC.相关的环境 ...

  8. Neo4j使用简单例子(转)

    Neo4j Versions Most of the examples on this page are written with Neo4j 2.0 in mind, so they skip th ...

  9. Mac 10.12安装Office 2011

    说明:由于WPS没有Mac版的,所以只能装这个,这个版本是2011的,基本够用了. 下载: (链接: https://pan.baidu.com/s/1pKGvXBP 密码: irw9)

  10. python-Condition 进程同步互斥

    #!/usr/bin/python import multiprocessing,time def A(cond): name=multiprocessing.current_process().na ...