Keywords Search (ac 自己主动机)
Keywords Search
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.
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
1
5
she
he
say
shr
her
yasherhs
3#include <iostream>
# include<cstring>
# include<cstdio>
# include<queue>
using namespace std; struct node
{
node *fail; //失败指针
node *next[26]; //Tire每一个节点的26个子节点(最多26个字母)
int count; //是否为该单词的最后一个节点
node(){ //构造函数初始化
fail=NULL;
count=0;
memset(next,NULL,sizeof(next));
}
}*q[1<<6|5];
char keyword[50+5]; //输入的单词
char str[1<<6|5]; //模式串 void insert(char *str, node *root) //建立字典树
{
node *p=root;
for(int i=0;str[i]!='\0';i++)
{
int id=str[i]-'a';
if(p->next[id]==NULL)
p->next[id]=new node();
p=p->next[id];
}
p->count++;
} void build_ac_automation(node *root)
{
queue<node *> Q;
int i;
root->fail=NULL;
Q.push(root);
while(!Q.empty())
{
node *temp=Q.front();
Q.pop();
node *p=NULL;
for(i=0;i<26;i++)
{
if(temp->next[i]!=NULL) //temp 为父结点
{
if(temp==root) temp->next[i]->fail=root;
else
{
p=temp->fail; // 思路的关键点,
while(p!=NULL)
{
if(p->next[i]!=NULL)
{
temp->next[i]->fail=p->next[i];
break;
}
p=p->fail; //p=p->fail也就是p=NULL
}
if(p==NULL) temp->next[i]->fail=root;
}
Q.push(temp->next[i]);
}
}
}
}
int query(node *root)
{
int i=0,cnt=0,len=strlen(str);
node *p=root;
for(int i=0;i<len;i++)
{
int id=str[i]-'a';
while(p->next[id]==NULL && p!=root) p=p->fail;
p=p->next[id];
p=(p==NULL)?root:p;
node *temp=p;
while(temp!=root && temp->count!=-1)
{
cnt+=temp->count;
temp->count=-1; //表示该单词已经出现过了。防止反复计数
temp=temp->fail; //temp指向e节点的失败指针所指向的节点继续查找
}
}
return cnt;
}
int main()
{
int n,t;
scanf("%d",&t);
while(t--)
{
node *root=new node();
scanf("%d",&n);
getchar();
while(n--)
{
gets(keyword);
insert(keyword,root);
}
build_ac_automation(root);
scanf("%s",str);
printf("%d\n",query(root));
}
return 0;
}
Keywords Search (ac 自己主动机)的更多相关文章
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDU 2222 Keywords Search AC自己主动机入门题
单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...
- hdu2222--Keywords Search+AC自己主动机模板
题目链接:pid=2222">点击进入 KMP对模式串进行处理.然后就能够方便的推断模式串是否在目标串中出现了:这显示适合一个模式串多个目标串的情况.可是假设模式串有多个,这时假设还用 ...
- hdoj 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自己主动机模板题)
题意:给出一个字符串和若干个模板,求出在文本串中出现的模板个数. 思路:由于有可能有反复的模板,trie树权值记录每一个模板出现的次数就可以. #include<cstdio> #incl ...
- AC自己主动机
AC自己主动机 AC自己主动机是KMP和Trie的结合,主要处理多模板串匹配问题.以下推荐一个博客,有助于学习AC自己主动机. NOTONLYSUCCESS 这里另一个Kuangbin开的比赛,大家 ...
- [POJ 1204]Word Puzzles(Trie树暴搜&AC自己主动机)
Description Word puzzles are usually simple and very entertaining for all ages. They are so entertai ...
- ZOJ - 3228 Searching the String (AC自己主动机)
Description Little jay really hates to deal with string. But moondy likes it very much, and she's so ...
- POJ 2778 DNA Sequence (AC自己主动机 + dp)
DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...
随机推荐
- 基于visual Studio2013解决面试题之1307二分查找
题目
- 阿根廷探戈舞会- 一起salsa百科 - 一起salsa网 - Powered by HDWiki!
阿根廷探戈舞会- 一起salsa百科 - 一起salsa网 - Powered by HDWiki! 阿根廷探戈舞会 编辑词条 发表评论(2) 目录 • 京城阿根廷探戈资源 • 上海阿根廷探戈 ...
- C++要点
以下的这些要点是对全部的C++程序猿都适用的.我之所以说它们是最重要的,是由于这些要点中提到的是你通常在C++书中或站点上无法找到的.如:指向成员的指针,这是很多资料中都不愿提到的地方, ...
- C#拖曳控件加载,bll报错问题
C#拖曳控件加载,bll报错问题,加载时实例如化bll时加上一个判断 if (!(GetService(typeof(IDesignerHost)) != null || Sys ...
- 熬之滴水成石:最想深入了解的内容--windows内核机制(15)
66--内存管理(4) 说说在windows中内存空间初始化的事,开始的开始通过处理器的分页机制,预先建立相应足够的页表以便页表来访问物理内存.预先建立的这个物理内存的是windows自己的加载程序, ...
- nginx+memcached+ftp上传图片+iis
nginx+memcached+ftp上传图片+iis 自毕业以来,一直在现在公司做订餐系统的开发,那会儿没有口碑,没有饿了么,更别说美团外卖,百度外卖了...因为规模都比较小,都是一个服务器包含数据 ...
- 减少HTTP请求之合并图片详解(大型网站优化技术)
原文:减少HTTP请求之合并图片详解(大型网站优化技术) 一.相关知识讲解 看过雅虎的前端优化35条建议,都知道优化前端是有多么重要.页面的加载速度直接影响到用户的体验.80%的终端用户响应时间都花在 ...
- Codility上的问题 (17) PI 2012
这个题比较简单,给定一个整数数组,对每个元素,求出和它最近比它大的数的距离(下标绝对值),如果没有比它大的数,认为距离是0. 数组元素个数 N [0..50000],数组元素范围[-10^9, +10 ...
- HDU1027 Ignatius and the Princess II 【next_permutation】【DFS】
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- Ubuntu 环境安装整理
Ubuntu11.04下Java开发环境搭建和配置 转自:http://guoyunsky.iteye.com/blog/1175861 类似的搭建,网上一搜一大把,但每次去搜索比较麻烦.我这里就整理 ...