Keywords Search
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 25356 Accepted Submission(s): 8280
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int INF=0x5fffffff;
const int MS=;
const double EXP=1e-; struct node
{
int have;//根据情况灵活变化
node * next[];
}nodes[MS*]; //注意这个大小 尽量大一点 node *root;
int cnt,ans; char text[MS*]; node * add_node(int c)
{
node *p=&nodes[c];
for(int i=;i<;i++)
p->next[i]=NULL;
p->have=;
return p;
} void insert(char *str)
{
node *p=root;
int len=strlen(str);
for(int i=;i<len;i++)
{
int id=str[i]-'a';
if(p->next[id]==NULL)
{
p->next[id]=add_node(cnt++);
}
p=p->next[id];
}
p->have++;
}
void search(char *str)
{
node *p=root;
int len=strlen(str);
for(int i=;i<len;i++)
{
int id=str[i]-'a';
p=p->next[id];
if(p==NULL)
return ;
if(p->have)
{
ans+=p->have;
p->have=;
}
}
} int main()
{
int n,i,T;
scanf("%d",&T);
while(T--)
{
cnt=;
ans=;
root=add_node(cnt++);
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%s",text);
insert(text);
}
scanf("%s",text);
int len=strlen(text);
for(i=;i<len;i++)
{
search(text+i);
}
printf("%d\n",ans);
}
return ;
}
这题是AC自动机最经典的入门题。学会了kmp,Trie,就可以学习ac自动机了。
time : 280 ms
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int INF=0x5fffffff;
const int MS=;
const double EXP=1e-;
// AC自动机 KMP TRIE
struct node
{
bool isbad;
node *pre;
node * next[];
int n;
}nodes[MS*]; //注意这个大小 个数*每个的长度就不会访问非法内存 node *root;
int cnt,ans; char text[MS*]; node * add_node(int c)
{
node *p=&nodes[c];
for(int i=;i<;i++)
p->next[i]=NULL;
p->isbad=false;
p->pre=NULL;
p->n=;
return p;
} void insert(char *str)
{
node *p=root;
int len=strlen(str);
for(int i=;i<len;i++)
{
int id=str[i]-'a';
if(p->next[id]==NULL)
{
p->next[id]=add_node(cnt++);
}
p=p->next[id];
}
p->isbad=true;
p->n++; //终止节点
} void build()
{ // 在trie树上加前缀指针
for(int i=;i<;i++)
nodes->next[i]=root;
nodes->pre=NULL;
root->pre=nodes;
deque<node *> dq;
dq.push_back(root);
while(!dq.empty())
{
node *proot=dq.front();
dq.pop_front();
for(int i=;i<;i++)
{
node *p=proot->next[i];
if(p!=NULL)
{
node *pre=proot->pre;
while(pre)
{
if(pre->next[i]!=NULL) //NULL==0
{
p->pre=pre->next[i];
if(p->pre->isbad)
p->isbad=true;
break;
}
else
pre=pre->pre;
}
dq.push_back(p);
}
}
}
} void search(char *str)
{ //返回值为true,说明包含模式串
node *p=root;
int len=strlen(str);
for(int i=;i<len;i++)
{
int id=str[i]-'a';
while(p!=root&&p->next[id]==NULL)
{
p=p->pre;
}
p=p->next[id];
if(p==NULL)
{
p=root;
}
node *tp=p;
while(tp!=root&&tp->n!=)
{
ans+=tp->n;
tp->n=;
tp=tp->pre;
} /*
while(1) 是否包含模式串
{
if(p->next[id])
{
p=p->next[id];
if(p->isbad)
return true;
break;
}
else
p=p->pre;
}
*/
} //return false;
} int main()
{
int n,T;
scanf("%d",&T);
while(T--)
{
cnt=;
ans=;
add_node(cnt++);
root=add_node(cnt++);
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%s",text);
insert(text);
}
build();
scanf("%s",text);
search(text);
printf("%d\n",ans);
}
return ;
}
/*
节点p的前缀指针定义为:指向树中出现
过的S的最长的后缀(不能等于S)。
如果p节点匹配失败,该节点对应c,就沿着它的父节点的前缀指针走,直到某节点,其儿子节点
对应的字母也为c,把p节点的前缀指针指向该儿子节点。如果走到了root都没有找到,就指向root
*/
Keywords Search的更多相关文章
- 【HDU2222】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- hdu2222 Keywords Search ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...
- HDU 2222 Keywords Search(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- C++之路进阶——hdu2222(Keywords Search)
/*Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- 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: 65536/32768 K (Java/Others)T ...
- hdu----(2222)Keywords Search(trie树)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
随机推荐
- vim编辑十六进制文件
首先用二进制方式打开 vim file -b 之后输入 :%!xxd 还原为二进制文件 :%!xxd -r
- c# 解决IIS写Excel的权限问题
c# 解决IIS写Excel的权限问题 from: http://www.jb51.net/article/31473.htm 发布:mdxy-dxy 字体:[增加 减小] 类型:转载 使用以上方法必 ...
- ELF学习--重定位文件
add.c int data = 1;int bss;const int rodata = 1;int add(int num1, int num2){ int sum = 0; sum = num1 ...
- Kafka学习记录
1 Kafka的基本介绍 Apache Kafka是分布式发布-订阅消息系统.它最初由LinkedIn公司开发,之后成为Apache项目的一部分.具有快速.可扩展.分布式.可复制等特点.Kafka与传 ...
- 使用spring.net 1.3.2框架部署在虚拟目录上发生错误
如果你的网站使用了Spring.net 1.3.2,并部署在IIS的虚拟目录上,那么将会出现如下错误: The virtual path '/currentcontext.dummy' maps ...
- JSP中的TAG
http://blog.csdn.net/hongweigg/article/details/12006849 JSP标签有两种实现方法,一种是使用tag 文件,一种是使用tld文件. 1.使用tag ...
- Binary Search
Binary Search [原文见:http://www.topcoder.com/tc?module=Static&d1=tuto ...
- WatchKit App Submission Issues
查看原文: http://leancodingnow.com/watchkit-app-submission-issues/ I submitted a new version of my app P ...
- 微吧里的各种margin负值
直在做各种项目接各种需求,但你的代码能力得到提高了吗?不停的项目经历虽然能够增加你的代码行数,但不一定能提升你的代码质量,所以除了构建阶段的代码细扣,项目之后的代码总结是至关重要的. 微吧中除了模块化 ...
- GridView实现多表头合并[转]
1.这里先介绍单纯的GridView多表头合并,先上图: 可以看到,上图就是生成的多表头,具体的后台代码是在Row_Created事件中创建的.先看创建代码: protected void GridV ...