AC日记——Keywords Search hdu 2222
2024-09-04 15:17:39
思路:
ac自动机模板题;
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 500005 struct TreeNodeType {
int count; TreeNodeType *fail; TreeNodeType *next[]; TreeNodeType()
{
fail=NULL,count=;
for(int i=;i<;i++) next[i]=NULL;
}
};
struct TreeNodeType *root,*que[maxn]; int n; char str[maxn*],word[]; inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
} void insert(char *ch)
{
int temp,len=strlen(ch);
TreeNodeType *p=root;
for(int i=;i<len;i++)
{
temp=ch[i]-'a';
if(p->next[temp]==NULL) p->next[temp]=new TreeNodeType;
p=p->next[temp];
}
p->count++;
} void build()
{
int tail=,head=;que[head]=root;
while(head<tail)
{
TreeNodeType *p=que[head++],*temp=NULL;
for(int i=;i<;i++)
{
if(p->next[i]==NULL) continue;
if(p==root) p->next[i]->fail=root;
else
{
temp=p->fail;
while(temp!=NULL)
{
if(temp->next[i]!=NULL)
{
p->next[i]->fail=temp->next[i];
break;
}
temp=temp->fail;
}
if(temp==NULL) p->next[i]->fail=root;
}
que[tail++]=p->next[i];
}
}
} int query()
{
int pos,len=strlen(str),res=;TreeNodeType *p=root;
for(int i=;i<len;i++)
{
pos=str[i]-'a';
while(p->next[pos]==NULL&&p!=root) p=p->fail;
if(p->next[pos]!=NULL) p=p->next[pos];else p=root;
TreeNodeType *temp=p;
while(temp!=root&&temp->count!=-) res+=temp->count,temp->count=-,temp=temp->fail;
}
return res;
} int main()
{
int T;
in(T);
while(T--)
{
root=new TreeNodeType;
in(n);for(int i=;i<=n;i++) gets(word),insert(word);
build(),scanf("%s",str),printf("%d\n",query());
}
return ;
}
最新文章
- 使用Monit监控本地进程
- bootstrap datetimepicker 中只显示年或者只显示月份
- mysql高可用之DRBD + HEARTBEAT + MYSQL
- MySQL的分页
- 在linux中的virtualbox无法挂载usb设备的解决方法
- 2016 Multi-University Training Contest 5 ATM Mechine
- ubuntu为Python添加默认搜索路径
- Fedora安装
- android--HttpURLConnection(转载)
- 【BZOJ1861】【splay】Book 书架
- USACO Milk2 区间合并
- .Net程序猿玩转Android开发---(7)相对布局RelativeLayout
- .net设计模式之装饰模式
- Git的安装使用和基本命令(一)
- 人工神经网络,支持任意数量隐藏层,多层隐藏层,python代码分享
- 腾讯技术分享:GIF动图技术详解及手机QQ动态表情压缩技术实践
- 计算机基础:计算机网络-chapter2
- Visual studio中编译和使用libpng和zlib
- STL基础--算法(不修改数据的算法)
- JQuery字符串的操作
热门文章
- vue.js的特点-1
- [STL] STL各容器实现原理
- 【bzoj1455】罗马游戏 可并堆+并查集
- 【bzoj2007】[Noi2010]海拔 最小割+对偶图+最短路
- [Leetcode] Path Sum II路径和
- wya费用流
- [学习笔记]可持久化数据结构——数组、并查集、平衡树、Trie树
- HZOI String STL的正确用法
- Codeforces Round #525 (Div. 2)B. Ehab and subtraction
- barba.js 优化页面跳转用户体验