hdu----(2222)Keywords Search(ac自动机)
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35930 Accepted Submission(s): 11597
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 case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
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.
5
she
he
say
shr
her
yasherhs
#define LOCAL
#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
struct Trie
{
struct Trie *fail;
struct Trie *child[];
int tail; //末尾标记
}; void _insert(char *s,Trie *root) //构造一个Trie树
{
Trie *newcur,*cur;
cur=root;
for(int i=;s[i];i++)
{
if(cur->child[s[i]-'a']==NULL)
{
newcur= new Trie ;
for(int j=;j<;j++)
newcur->child[j]=NULL;
newcur->fail=NULL;
newcur->tail=;
cur->child[s[i]-'a']=newcur;
}
cur=cur->child[s[i]-'a'];
}
cur->tail++; //有可能有重复的单词
} //构造失败指针
void ac_fail(Trie * root)
{
queue<Trie*>tree;
Trie *fro,*q;
tree.push(root);
while(!tree.empty())
{
fro=tree.front();
tree.pop();
for(int i=;i<;i++){
if(fro->child[i]!=NULL)
{
if(fro==root)
fro->child[i]->fail=root; //将他的下一个函数的指针的失败指针指向当前指针
else
{
q=fro;
while(q->fail)
{
if(q->fail->child[i]){
fro->child[i]->fail=q->fail->child[i];
break;
}
q=q->fail;
}
if(!q->fail) fro->child[i]->fail=root;
}
tree.push(fro->child[i]);
}
}
}
} int query(char *s,Trie *root)
{
Trie *cur=root,*newcur;
int ans=;
for(int i=;s[i];i++)
{
while(cur->child[s[i]-'a']==NULL&&cur!=root)
cur=cur->fail;
cur=cur->child[s[i]-'a'];
if(cur==NULL) cur=root;
newcur=cur;
while(newcur!=root&&newcur->tail>)
{
ans+=newcur->tail;
newcur->tail=;
newcur=newcur->fail;
}
}
return ans;
}
char s1[];
char t1[]; //目标主串
int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
int cas,n;
Trie *root;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
root= new Trie;
for(int i=;i<;i++)
root->child[i]=NULL;
root->fail=NULL;
root->tail=;
while(n--)
{
scanf("%s",s1);
_insert(s1,root);
}
ac_fail(root);
scanf("%s",t1);
printf("%d\n",query(t1,root));
}
return ;
}
hdu----(2222)Keywords Search(ac自动机)的更多相关文章
- hdu 2222 Keywords Search——AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)
题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- hdu 2222 Keywords Search - Aho-Corasick自动机
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- hdoj 2222 Keywords Search(AC自动机)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路分析:该问题为多模式匹配问题,使用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 ...
随机推荐
- Python import 指定目录中的模块
#coding=utf-8 import os,sys sys.path.append('test') # 下级目录(text) parentdir = os.path.dirname(os.path ...
- Python串口编程
python的串口网上有很多例子,这里了只是把认为好的整理到一起. 首先,应该安装serial模块,还能开始后续的操作.我用的python2.6,serial模块可以在这里下载安装serial模块下载 ...
- [Effective Java]第五章 泛型
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- CA*Layer(CAReplicatorLayer--)
CAReplicatorLayer (反射应用) 指定一个继承于UIView的ReflectionView,它会自动产生内容的反射效果: + (Class)layerClass//我们也可以通过重写V ...
- HDU 5813 Elegant Construction(优雅建造)
HDU 5813 Elegant Construction(优雅建造) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65 ...
- 6.mybatis异常:SQL Mapper Configuration,Error parsing Mapper XML,Could not resolve type alias
在xxxMapper中 <select id="getClazz" parameterType="int" resultType="getCla ...
- VirtualBox全屏切换
用VirtualBox的时候,如果设置为全屏,想再切换回来,没有什么菜单,只有通过键盘的快捷键来操作,才可以恢复. 我常常忘掉,所以老是得去找,以后需要记住这几个按键的快捷键. 1.全屏与非全屏切换: ...
- Android实现简单短信发送器
布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to ...
- org.apache.http.client.CircularRedirectException: Circular redirect to "http://xxx"问题解决
org.apache.http.client.CircularRedirectException: Circular redirect to "http://xxx"问题解决 ...
- ssis freach loop container 传入变量给 某些数据源的时候。
ssis freach loop container 传入变量给 某些数据源的时候. 应该选择loop container ,设置delayvalidateion为true. 这样数据源控件就不会报e ...