hdu3695 AC自动机优化
题目链接: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自动机优化的更多相关文章
- 【uva11019-Matrix Matcher】AC自动机+优化+记录
http://acm.hust.edu.cn/vjudge/problem/33057 题意:在二维文本串T中查找一个二维模板串P出现了多少次. 题解: 拆分模板串P的每一行,建AC自动机.拆分文本串 ...
- HDU3695(AC自动机模板题)
题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...
- hdu3695 ac自动机入门
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- POJ 3987 Computer Virus on Planet Pandora (AC自动机优化)
题意 问一个字符串中包含多少种模式串,该字符串的反向串包含也算. 思路 解析一下字符串,简单. 建自动机的时候,通过fail指针建立trie图.这样跑图的时候不再跳fail指针,相当于就是放弃了fai ...
- 洛谷 P3808 【模板】AC自动机(简单版) (AC自动机优化板子)
题中有一个坑点,就是模式串可以相同,并且全部计数. #include <bits/stdc++.h> using namespace std; const int maxn=1e6+10; ...
- 【bzoj3940】[Usaco2015 Feb]Censoring AC自动机
题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they h ...
- [BZOJ 1009] [HNOI2008] GT考试 【AC自动机 + 矩阵乘法优化DP】
题目链接:BZOJ - 1009 题目分析 题目要求求出不包含给定字符串的长度为 n 的字符串的数量. 既然这样,应该就是 KMP + DP ,用 f[i][j] 表示长度为 i ,匹配到模式串第 j ...
- AC自动机学习笔记-2(Trie图&&last优化)
我是连月更都做不到的蒟蒻博主QwQ 考虑到我太菜了,考完noip就要退役了,所以我决定还是把博客的倒数第二篇博客给写了,也算是填了一个坑吧.(最后一篇?当然是悲怆のnoip退役记啦QAQ) 所以我们今 ...
- [BJOI2011]禁忌 --- AC自动机 + 矩阵优化 + 期望
bzoj 2553 [BJOI2011]禁忌 题目描述: Magic Land上的人们总是提起那个传说:他们的祖先John在那个东方岛屿帮助Koishi与其姐姐Satori最终战平.而后,Koishi ...
随机推荐
- 吴裕雄--天生自然 python数据分析:葡萄酒分析
# import pandas import pandas as pd # creating a DataFrame pd.DataFrame({'Yes': [50, 31], 'No': [101 ...
- 教你win7关闭开机动画,大幅度加快开机时间
Win7系统如何关闭开机动画 Win7系统开机动画关闭教程,以前我们说过很多种帮助Win7开机加速的方法,比如减少Win7开机启动的程序.服务或计划任务等.不过这些都优化都是针对已经进入Win7系统后 ...
- 2016/11/10 吃吃喝喝Hacking Thursday Night聚餐活动 at Dunkin Donuts
店名:Dunkin Donuts 唐恩都乐 点评:http://www.dianping.com/shop/21378231 地址:静安区南京西路1649号静安公园内(近静安公园) 走法:地铁2号线静 ...
- “一亿”的教训:一次Google信箱诈骗是如何得手的?
网络诈骗是指以非法占有为目的,利用互联网采用虚构事实或者隐瞒真相的方法,骗取数额较大的公私财物的行为.一年比一年网络诈骗越来越高手段,可以说是日益猖獗.在这里提醒一次各位朋友一定要注意自己的网络安全. ...
- C++走向远洋——27(项目三,时间类)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:time.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- win10下安装LoadRunner12汉化包
1.前提是已经下载LoadRunner安装文件,及已经安装成功: 安装包: 安装成功后,桌面会出现3个图标: 下面,开始安装汉化包: 1.右键点击“HP_LoadRunner_12.02_Commun ...
- Py基础之函数
'''函数是指一类同类事物的抽象,而且这种抽象可以拓展,并且可以用在同一类事物上'''print (abs(-100),abs(100)) #abs函数是python内置的函数,可以用来求绝对值#pr ...
- 前端每日实战:145# 视频演示如何用纯 CSS 创作一个电源开关控件
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/PdMyJd 可交互视频 此视频是可 ...
- 符合SEO的HTML布局规范
少用例如iframe等标签引入内容,可以不用尽量不用,因为搜索引擎无法搜索到框架里面的内容: <!--页面注解--> <html> <head> <title ...
- Android html5 控制video currentTime不准确,精确,解决办法。
早在flash时代 我们控制视频播放指定时间位置的画面也会有不准确的情况, 具体情况表现为:video.seek(time) 而实际画面会跳到此时间附近(1-2秒)的画面 而HTML5 我们通过 ...