题目链接:http://icpc.njust.edu.cn/Problem/Hdu/3695/

不加last指针的AC自动机会T,原因是他费了很多功夫在跳转上,而last指针是直接直到跳转的终止位置,直接跳转到终止位置上,所以只有一次跳转。未优化的fail指针最坏的情况下复杂度是O(|S|*|T|),其中|S|是模式串长度的均值,|T|是文本串的 长度。

代码如下:

 #include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
const int INF = 0x3f3f3f3f;
const LL mod = 1e9+;
typedef pair<int,int> pll;
const int N = 1e6+;
int tot = ;
char str[N*], tmp[N*];
int trie[N*][], cnt[N*], fair[N*];
void init(){
for(int i = ; i < tot; i++){
for(int j = ; j < ; j++)
trie[i][j] = ;
}
tot = ;
}
void Insert(){
int rt = , len = strlen(str);
for(int i = ; i < len; i++){
int id = str[i] - 'A';
if(!trie[rt][id]) {
cnt[tot] = ;
fair[tot] = ;
trie[rt][id] = tot++;
}
rt = trie[rt][id];
}
cnt[rt]++;
}
void Build_tree(){
queue<int> q;
q.push();
int p;
while(!q.empty()){
int tmp = q.front();
q.pop();
for(int j = ; j < ; j++){
if(trie[tmp][j]){
if(tmp == )
fair[trie[tmp][j]] = ;
else {
p = fair[tmp];
while(p){
if(trie[p][j]){
fair[trie[tmp][j]] = trie[p][j];
break;
}
else p = fair[p];
}
if(!p) fair[trie[tmp][j]] = ;
}
q.push(trie[tmp][j]);
}
}
}
}
int Find(int len){
int ret = , rt = ;
for(int i = ; i < len; i++){
int id = str[i] - 'A';
while(rt != && !trie[rt][id]) rt = fair[rt];;
if(trie[rt][id]) rt = trie[rt][id];
int p = rt;
while(p != ){
if(cnt[p] >= ){
ret += cnt[p];
cnt[p] = -;
}
else break;
p = fair[p];
}
}
rt = ;
for(int i = len - ; i >= ; i--){
int id = str[i] - 'A';
while(rt != && !trie[rt][id]) rt = fair[rt];
if(trie[rt][id]) rt = trie[rt][id];
int p = rt;
while(p != ){
if(cnt[p] >= ){
ret += cnt[p];
cnt[p] = -;
}
else break;
p = fair[p];
}
}
return ret;
}
int main(){
int t;
scanf("%d", &t);
while(t--){
int n;
init();
scanf("%d", &n);
while(n--){
scanf("%s", str);
Insert();
}
Build_tree();
scanf("%s", tmp);
int len = strlen(tmp);
int ccc = ;
for(int i = ; i < len; i++){
if(tmp[i] != '['){
str[ccc++] = tmp[i];
}
else {
int _c = ;
i++;
while(isdigit(tmp[i]))
_c = _c * + (int)(tmp[i]-''), i++;
while(_c--)
str[ccc++] = tmp[i];
i++;
}
}
printf("%d\n", Find(ccc));
}
return ;
}

hdu3695 AC自动机优化的更多相关文章

  1. 【uva11019-Matrix Matcher】AC自动机+优化+记录

    http://acm.hust.edu.cn/vjudge/problem/33057 题意:在二维文本串T中查找一个二维模板串P出现了多少次. 题解: 拆分模板串P的每一行,建AC自动机.拆分文本串 ...

  2. HDU3695(AC自动机模板题)

    题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...

  3. hdu3695 ac自动机入门

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  4. POJ 3987 Computer Virus on Planet Pandora (AC自动机优化)

    题意 问一个字符串中包含多少种模式串,该字符串的反向串包含也算. 思路 解析一下字符串,简单. 建自动机的时候,通过fail指针建立trie图.这样跑图的时候不再跳fail指针,相当于就是放弃了fai ...

  5. 洛谷 P3808 【模板】AC自动机(简单版) (AC自动机优化板子)

    题中有一个坑点,就是模式串可以相同,并且全部计数. #include <bits/stdc++.h> using namespace std; const int maxn=1e6+10; ...

  6. 【bzoj3940】[Usaco2015 Feb]Censoring AC自动机

    题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they h ...

  7. [BZOJ 1009] [HNOI2008] GT考试 【AC自动机 + 矩阵乘法优化DP】

    题目链接:BZOJ - 1009 题目分析 题目要求求出不包含给定字符串的长度为 n 的字符串的数量. 既然这样,应该就是 KMP + DP ,用 f[i][j] 表示长度为 i ,匹配到模式串第 j ...

  8. AC自动机学习笔记-2(Trie图&&last优化)

    我是连月更都做不到的蒟蒻博主QwQ 考虑到我太菜了,考完noip就要退役了,所以我决定还是把博客的倒数第二篇博客给写了,也算是填了一个坑吧.(最后一篇?当然是悲怆のnoip退役记啦QAQ) 所以我们今 ...

  9. [BJOI2011]禁忌 --- AC自动机 + 矩阵优化 + 期望

    bzoj 2553 [BJOI2011]禁忌 题目描述: Magic Land上的人们总是提起那个传说:他们的祖先John在那个东方岛屿帮助Koishi与其姐姐Satori最终战平.而后,Koishi ...

随机推荐

  1. figure设置坐标轴

    import matplotlib.pyplot as plt import numpy as np x=np.linspace(-3,3,50) y1=x*2+1 y2=x**2 plt.plot( ...

  2. 添砖加瓦:[OpenCV]入门(一)

    1.OpenCV安装 (1)下载: 本文采用的是源码的方式进行安装,源码可以从OpenCV官网下载.这里以3.4.1为例. (2)安装 这里下载到的文件为3.4.1.zip."unzip 3 ...

  3. python设置检查点简单实现

    说检查点,其实就是对过去历史的记录,可以认为是log.不过这里进行了简化.举例来说,我现在又一段文本.文本里放有一堆堆的链接地址.我现在的任务是下载那些地址中的内容.另外因为网络的问题或者网站的问题, ...

  4. pymongo bugfix后记

    有网友反馈py-mongo-sync同步异常,检查发现curosr[0]取查询结果第一个文档时报错"no such item for Cursor instance". 这里的逻辑 ...

  5. Go技术日报(2020-02-28)

    go 语言中文网(每日资讯)_2020-02-28 一.Go 语言中文网 Gopher 学习效率低怎么办?曹大谈工程师应该怎么学习 Go 的 http 包中默认路由匹配规则 [每日一库]Web 表单验 ...

  6. 利用python代码操作git

    python操作git 安装模块 pip3 install gitpython 基本使用 import os from git.repo import Repo # 创建本地路径用来存放远程仓库下载的 ...

  7. ZYNQ自定义AXI总线IP应用——PWM实现呼吸灯效果

    一.前言 在实时性要求较高的场合中,CPU软件执行的方式显然不能满足需求,这时需要硬件逻辑实现部分功能.要想使自定义IP核被CPU访问,就必须带有总线接口.ZYNQ采用AXI BUS实现PS和PL之间 ...

  8. pc端适配移动端

    pc端和移动端共用一套代码 1. 允许网页宽度自动调整 在网页代码的头部,加入一行viewport元标签 <meta name="viewport" content=&quo ...

  9. d3.js ---画坐标轴

    画坐标轴 //使用d3的svg的axis()方法生成坐标轴 var x_axis = d3.svg.axis().scale(scale_x), y_axis = d3.svg.axis().scal ...

  10. css 进度条的文字根据进度渐变

    需求 1.进度条里面的文字需要根据进度的长度而变化 原理 用两个一模一样的样式的 div 重叠起来 效果 字体开始为 蓝色,跟随进度条变为 白色 在线预览: https://jsfiddle.net/ ...