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-6-Z字形变换
题目描述: 将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数: P A H N A P L S I I G Y I R 之后从左往右,逐行读取字符:" ...
- 44.oracle表空间的使用
要给下属充分的发挥空间,要允许下属犯错,下属犯错自己能顶下来就顶着,不要盯得让下属觉得“这不是我的事,我只是个小打工的”,团建要放在首位.不可在下属面前“装B”,别人也不傻. Oracle数据库开创性 ...
- QuantLib 金融计算——基本组件之天数计算规则详解
目录 天数计算规则详解 定义 30 / 360 法 30/360 US 30/360 Bond Basis 30E/360 30E/360 ISDA Actual 法 Actual/Actual IC ...
- jQuery 发送 ajax json 请求。。
$.extend({ postJson: function (data) { data = data || {} $.ajax({ type: "POST", url: data. ...
- 一些很好用但不常用的css属性总结 (持续中......)
在各种框架横行的9012,感觉我们学前端的都变得浮躁了很多,放一张最近流行的神图: 唉,扯远了, 还是整理我的东西吧,路漫漫其修远兮,吾将上下而求索! 1, position:sticky; 粘性定位 ...
- Bluetooth Lowe Energy
BTL---------- // Wikipedia --------The first review paper to read when you counterred a new filed . ...
- [转] Linux History(历史)命令用法 15 例
[From]https://linuxtoy.org/archives/history-command-usage-examples.html 如果你经常使用 Linux 命令行,那么使用 histo ...
- Python SSLError
最近老是遇到这个问题. SSLError(SSLError(1, '[SSL: CERTIFIC ATE_VERIFY_FAILED] certificate verify failed (_ssl. ...
- window7 下安卓开发环境搭建
最新Win7下配置搭建安卓开发环境 注意:因为墙的原因 google的更新服务器需要改 hosts 你懂的.. 74.125.237.1 dl-ssl.google.com 不行就VPN ...
- (转)Mysql主从复制搭建及详解
http://www.cnblogs.com/kevingrace/p/6256603.html---------Mysql主从同步(1)-主从/主主环境部署梳理 原文:http://blog.csd ...