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自动机模板的更多相关文章

  1. HDU:2222-Keywords Search(AC自动机模板,匹配模拟)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) P ...

  2. KMP,Trie,AC自动机题目集

    字符串算法并不多,KMP,trie,AC自动机就是其中几个最经典的.字符串的题目灵活多变也有许多套路,需要多做题才能体会.这里收集了许多前辈的题目做个集合,方便自己回忆. KMP题目:https:// ...

  3. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  4. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

  5. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  6. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

  7. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

  8. HDU 2222 (AC自动机模板题)

    题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...

  9. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

随机推荐

  1. python-对象方法、静态方法、类方法

    #-*- coding:utf-8 -*- #本次学习:对象方法.静态方法.类方法 class SeniorTestingEngineer: #属性--只能对象来调用self.salary work_ ...

  2. TessorFlow学习 之 神经网络的构建

    1.建立一个神经网络添加层 输入值.输入的大小.输出的大小和激励函数 学过神经网络的人看下面这个图就明白了,不懂的去看看我的另一篇博客 def add_layer(inputs , in_size , ...

  3. ELK学习博客

    ELK实时日志分析平台环境部署--完整记录 https://www.cnblogs.com/kevingrace/p/5919021.html

  4. Java执行js代码

    在做项目中有时候需要用到Java调用js文件执行相应的方法 在JDK1.6添加了新的ScriptEngine类,允许用户直接执行js代码. import org.junit.Test; import ...

  5. 《算法》第四章部分程序 part 6

    ▶ 书中第四章部分程序,加上自己补充的代码,图的环相关 ● 无向图中寻找环 package package01; import edu.princeton.cs.algs4.In; import ed ...

  6. leetcode1019

    class Solution(object): def nextLargerNodes(self, head: ListNode) -> 'List[int]': n = 0 temp = he ...

  7. ScheduledThreadPoolExecutor 使用线程池执行定时任务

    转自:https://segmentfault.com/a/1190000008038848 在现实世界里,我们总是免不了要定期去做一件事情(比如上课)—— 在计算机的世界里,更是如此.比如我们手机每 ...

  8. 解决Run As -> Java Application不能运行问题

    转自:https://breakshell.iteye.com/blog/467130 点 Run As -> Java Application 不能运行,报的错误如下: Plug-in org ...

  9. mysql 索引,转载

    from:http://blog.csdn.net/zhanglu0223/article/details/8713149 1. 索引建立的原则 用于索引的最好的备选数据列是那些出现在WHERE子句. ...

  10. jvm问题

     问题: 1. 一台服务器,部署多个服务,请问,这多个服务,对应的是一个jvm,还是多个jvm? 2. 一个线程,从controller 到 service,到DAO,会调用多个方法,请问是 对应一个 ...