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数组),其作用就是在 ...
随机推荐
- leetcode-479-Largest Palindrome Product(找到两个乘数相乘得到的最大的回文数)
题目描述: Find the largest palindrome made from the product of two n-digit numbers. Since the result cou ...
- jQuery 获取元素当前位置offset()与position()
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...
- jquery scrollTop()与scrollLeft()
1.scrollLeft() scrollLeft() 方法设置或返回被选元素的水平滚动条位置. 提示:当滚动条位于最左侧时,位置是 0. 当用于返回位置时:该方法返回第一个匹配元素的滚动条的水平位置 ...
- ReactNative常用组件库 victory-native 图表
victory-native 是不错的图表组件,支持很多种图表 地址: https://github.com/FormidableLabs/victory-native 先安装 react-nativ ...
- vue进行路由拼图的使用案例
实现思路,利用路由进行实现多个组件拼图: Detail.vue <template> <div> <h1>详细展示</h1> <div>鞍山 ...
- Pycharm 报错 AttributeError: module 'pip' has no attribute 'main'
1.打开文件packaging_tool.py: D:\Program files\pycharm\PyCharm 2016.3.2\helpers\packaging_tool.py 2.添加导入: ...
- SPB 公开课记录
sql server 用工具到处mysql脚本,不过目前有点问题
- Touch事件机制
1.概念 Touch事件分发中有三个主角:Activity.ViewGroup和View.Activity的Touch事件事实上是调用它内部的ViewGroup的Touch事件,可以直接当成ViewG ...
- java值传递与引用传递
看代码: import java.lang.reflect.Field; public class Test { public static void main(String[] args) { In ...
- AngularJS 的常用特性(一)
前言:AngularJS 是一款来自 Google 的前端 JS 框架,该框架已经被应用到了 Google 的多款产品中,这款框架最核心特性有:MVC.模块化.自动化双向数据绑定.语义化标签.依赖注入 ...