KMP与AC自动机模板
HDU 1711 Number Sequence(KMP模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1711
#include<bits/stdc++.h>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pqueue priority_queue
#define NEW(a,b) memset(a,b,sizeof(a))
const double pi=4.0*atan(1.0);
const double e=exp(1.0);
const int maxn=1e6+;
typedef long long LL;
typedef unsigned long long ULL;
//typedef pair<LL,LL> P;
const LL mod=1e9+;
using namespace std;
int a[maxn],b[maxn],Next[maxn];
int n,m;
void get_next(){
Next[]=-;
int k=-,i=;
while(i<m){
while(k>-&&b[k]!=b[i]){
k=Next[k];
}
if(b[k]==b[i]||k==-){
k++;
}
Next[++i]=k;
}
return;
}
int main(){
fio;
int t;
cin>>t;
while(t--){
cin>>n>>m;
for(int i=;i<n;i++){
cin>>a[i];
}
for(int i=;i<m;i++){
cin>>b[i];
}
get_next();
int p=;
int is=-;
for(int i=;i<n;){
if(a[i]==b[p]||p==-){
p++;
i++;
}
else{
while(p!=-){
if(b[p]==a[i]){
break;
}
p=Next[p]; }
}
if(p==m){
is=i-m+;
break;
}
}
cout<<is<<endl;
}
}
HDU 2222 Keywords Search(AC自动机模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=2222
#include<bits/stdc++.h>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pqueue priority_queue
#define NEW(a,b) memset(a,b,sizeof(a))
const double pi=4.0*atan(1.0);
const double e=exp(1.0);
const int maxn=1e6+;
typedef long long LL;
typedef unsigned long long ULL;
//typedef pair<LL,LL> P;
const LL mod=1e9+;
using namespace std;
struct node{
node *nxt[];
node *fail;
int is;
void init(){
is=;
fail=;
NEW(nxt,);
}
};
node *root;
node *q[maxn];
int cnt;
void Insert(string s){
node *p=root;
int x;
for(int i=;s[i];i++){
x=s[i]-'a';
if(p->nxt[x]==NULL){
p->nxt[x]=new node;
p->nxt[x]->init();
}
p=p->nxt[x];
}
p->is++;
}
void build_failpoint(){
int head=;
int tail=;
q[]=root;
node *tmp;
node *f;
while(head<tail){
tmp=q[head++];
for(int i=;i<;i++){
if(tmp->nxt[i]){
if(tmp==root){
tmp->nxt[i]->fail=root;
}
else{
f=tmp->fail;
while(f!=){
if(f->nxt[i]!=){
tmp->nxt[i]->fail=f->nxt[i];
break;
}
f=f->fail;
}
if(f==){
tmp->nxt[i]->fail=root;
}
}
q[tail++]=tmp->nxt[i];
}
}
}
}
void ac_automation(string t){
node *p=root;
int x;
for(int i=;t[i];i++){
x=t[i]-'a';
if(p->nxt[x]){
p=p->nxt[x];
}
else{
p=p->fail;
while(p!=){
if(p->nxt[x]){
p=p->nxt[x];
break;
}
p=p->fail;
}
if(p==){
p=root;
}
}
node *tmp=p;
while(tmp->is!=-&&tmp!=root){
cnt+=tmp->is;
tmp->is=-;
tmp=tmp->fail;
}
}
}
int main(){
fio;
int t;
int n;
cin>>t;
string s;
while(t--){
cin>>n;
cnt=;
root=new node;
root->init();
for(int i=;i<n;i++){
cin>>s;
Insert(s);
}
build_failpoint();
cin>>s;
ac_automation(s);
cout<<cnt<<endl;
}
}
AC自动机改成了数组版(表示作为下标党用不惯指针)
#include<bits/stdc++.h>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pqueue priority_queue
#define NEW(a,b) memset(a,b,sizeof(a))
const double pi=4.0*atan(1.0);
const double e=exp(1.0);
const int maxn=1e6+;
typedef long long LL;
typedef unsigned long long ULL;
//typedef pair<LL,LL> P;
const LL mod=1e9+;
using namespace std;
struct node{
int nxt[];
int fail;
int is;
void init(){
is=;
fail=;
NEW(nxt,);
}
}q[maxn];
int root=,tot=;
int cnt,que[maxn];
void Insert(string s){
int p=root;
int x;
for(int i=;s[i];i++){
x=s[i]-'a';
if(q[p].nxt[x]==){
q[p].nxt[x]=++tot;
q[q[p].nxt[x]].init();
}
p=q[p].nxt[x];
}
q[p].is++;
}
void build_failpoint(){
int head=;
int tail=;
que[]=root;
int tmp;
int f;
while(head<tail){
tmp=que[head++];
for(int i=;i<;i++){
if(q[tmp].nxt[i]){
if(tmp==root){
q[q[tmp].nxt[i]].fail=root;
}
else{
f=q[tmp].fail;
while(f!=){
if(q[f].nxt[i]!=){
q[q[tmp].nxt[i]].fail=q[f].nxt[i];
break;
}
f=q[f].fail;
}
if(f==){
q[q[tmp].nxt[i]].fail=root;
}
}
que[tail++]=q[tmp].nxt[i];
}
}
}
}
void ac_automation(string t){
int p=root;
int x;
for(int i=;t[i];i++){
x=t[i]-'a';
if(q[p].nxt[x]){
p=q[p].nxt[x];
}
else{
p=q[p].fail;
while(p!=){
if(q[p].nxt[x]){
p=q[p].nxt[x];
break;
}
p=q[p].fail;
}
if(p==){
p=root;
}
}
int tmp=p;
while(q[tmp].is!=-&&tmp!=root){
cnt+=q[tmp].is;
q[tmp].is=-;
tmp=q[tmp].fail;
}
}
}
int main(){
fio;
int t;
int n;
cin>>t;
string s;
while(t--){
cin>>n;
cnt=;
q[root].init();
for(int i=;i<n;i++){
cin>>s;
Insert(s);
}
build_failpoint();
cin>>s;
ac_automation(s);
cout<<cnt<<endl;
}
}
KMP与AC自动机模板的更多相关文章
- HDU:2222-Keywords Search(AC自动机模板,匹配模拟)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) P ...
- KMP,Trie,AC自动机题目集
字符串算法并不多,KMP,trie,AC自动机就是其中几个最经典的.字符串的题目灵活多变也有许多套路,需要多做题才能体会.这里收集了许多前辈的题目做个集合,方便自己回忆. KMP题目:https:// ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- HDU 2222 (AC自动机模板题)
题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
随机推荐
- SpringBoot 实现前后端分离的跨域访问(CORS)
序言:跨域资源共享向来都是热门的需求,使用CORS可以帮助我们快速实现跨域访问,只需在服务端进行授权即可,无需在前端添加额外设置,比传统的JSONP跨域更安全和便捷. 一.基本介绍 简单来说,CORS ...
- 公式for TinyMCE 编辑器@ cnblogs.com
编辑器截图: 行内公式:\( f(x,y,z) = 3y^2 z \left( 3 + \frac{7x+5}{1 + y^2} \right) \) 行间公式:\\( f(x,y,z) = 3 ...
- Spring MVC 学习笔记9 —— 实现简单的用户管理(4)用户登录显示局部异常信息
Spring MVC 学习笔记9 -- 实现简单的用户管理(4.2)用户登录--显示局部异常信息 第二部分:显示局部异常信息,而不是500错误页 1. 写一个方法,把UserException传进来. ...
- 16.Ubuntu安装mysql及win7安装mysql
Ubuntu: 链接地址:https://www.cnblogs.com/logaa/p/6791819.html win7: 链接地址:https://jingyan.baidu.com/artic ...
- Maven项目的生命周期
Maven中存在三套生命周期,每一套生命周期相互独立,互不影响.在一套生命周期内,执行后面的命令前面的命令会自动执行. CleanLifeCycle:清理生命周期 mvn clean DefaultL ...
- JavaScript实现图片裁剪预览效果~(第一个小玩具)
感觉开始学习的前一个月真的太不珍惜慕课网的资源了 上面蛮多小玩意真的蛮适合我这样刚入门JavaScript的同学加深使用理解 大概收藏了百来门或大或小的课程 有一个感觉就是学这个真的比光是看书看概 ...
- 43. 根据某个字段查询的重复的记录,例:查testId重复的记录
select t.* from test t where t.testId in (select testIdfrom test group by testIdhaving count(test ...
- 【ASP.NET 插件】分享一款富文本web编辑器UEditor
UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码... <%@ Page Language ...
- linux常用工具及命令
1.windows复制文件到linux可以使用工具winscp工具 2.建立软连接命令(将/software/run.log的文件指向/usr/local/logs/中): cd /usr/local ...
- SecureCRT显示乱码的解决办法
下面来看看SecureCRT的显示出现乱码这种情况.比如: 现在我们重新设置一下 设置下图中的配置 1.选择字符编码为UTF-8. 2.设置字符集为GB2312后保存好后确认退出. 3.再次测试一下.