HDU P2222 Keywords Search
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的更多相关文章
- HDU 2222 Keywords Search(查询关键字)
HDU 2222 Keywords Search(查询关键字) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- HDU 2222 Keywords Search(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDU 2222 Keywords Search(瞎搞)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu 2222 Keywords Search 模板题
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu 2222 Keywords Search - Aho-Corasick自动机
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- 【刷题】HDU 2222 Keywords Search
Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...
- hdu 2222 Keywords Search
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路:裸AC自动机,直接贴代码做模板 #include<stdio.h> #includ ...
- HDU 2222 Keywords Search (AC自动机)
题意:给你一些模式串,再给你一串匹配串,问你在匹配串中出现了多少种模式串,模式串可以相同 AC自动机:trie树上进行KMP.首先模式串建立trie树,再求得失配指针(类似next数组),其作用就是在 ...
随机推荐
- graphviz画图与中文乱码等问题总结
最近想写一些文档,画一些程序的逻辑图,用了vision,markdown等软件感觉不怎么好用,于是找到graphviz,这款强大的软件.下面介绍一些入门,还有自己在用的过程中遇到的问题 1.中文乱码的 ...
- asp.net c# 虾米音乐API
最近用到虾米音乐的功能,主要是做一个分享音乐功能,找到好多代码,但是比较杂,有用的很少,因 此在此记录下,方便以后自己使用. 对于第三方网站,只要获取了唯一标识,基本上能抓取一些信息. 虾米 音乐的I ...
- Bootstrap Table使用方法详解
http://www.jb51.net/article/89573.htm bootstrap-table使用总结 bootstrap-table是在bootstrap-table的基础上写出来的,专 ...
- python 第一天学习(画个正方体)
import turtleturtle.goto(200,0)turtle.goto(200,200)turtle.goto(0,200)turtle.goto(0,0)turtle.penup()t ...
- P2278 操作系统
P2278 操作系统 题目描述 写一个程序来模拟操作系统的进程调度.假设该系统只有一个CPU,每一个进程的到达时间,执行时间和运行优先级都是已知的.其中运行优先级用自然数表示,数字越大,则优先级越高. ...
- ArrayList分析
ArrayList概述 ArrayList继承了AbstractList,实现了List接口,底层基于动态数组,容量大小可以动态变化,ArrayList中可以添加null元素,另外,ArrayList ...
- HDU 4508 湫湫系列故事——减肥记I
原题链接:点击此处 解题思路: 思路与01背包差不多,思路用二维数组表示: dp[i][v]=max{dp[i-1][v-k*b[i]]+k*a[i]|0<=k*b[i]<=v} 其dp( ...
- Sublime Text 3安装插件(Mac 10.12)
1.先安装Package Control,默认这个是没有安装的. 使用[control + -]打开控制台,输入以下代码: import urllib.request,os; pf = 'Packag ...
- PHPStorm操作小技巧
1.围绕选中字符输入引号或者括号 2.设置服务器部署 3.隐藏Project快捷键 Shift + Esc 4.IDE内窗口切换 Ctrl + TAB 5.关闭当前项目 File -> Clos ...
- ListView性能优化——convertView&viewHolder
ListView优化大致从以下几个角度:1.复用已经生成的convertView:2.添加viewHolder类:3.缓存数据(图片缓存):4.分页加载. 具体方案: 1.如果自定义适配器,那么在ge ...