HDU 4787 GRE Revenge
Now Coach Pang is preparing for the Graduate Record Examinations as George did in 2011. At each day, Coach Pang can:
- "+\(w\)": learn a word \(w\)
- "?\(p\)": read a paragraph \(p\), and count the number of learnt words. Formally speaking, count the number of substrings of \(p\) which is a learnt words.
Given the records of N days, help Coach Pang to find the count. For convenience, the characters occured in the words and paragraphs are only '0' and '1'.
强制在线。
考虑暴力根号筹够,每 \(\sqrt{n}\) 个重建 AC 自动机。
开两个Ac自动机,一个拿来存最近根号个,每次插入重构。另一个每个根号个就插入重构。
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5,M=5e6+5;
typedef long long LL;
int q[N],l,r,m,t,n,ln[N];
LL ls;
char str[N],ss[M],st[M];
void shift(char ss[])
{
int m=strlen(ss);
for(int i=ls%m;i<m;i++)
st[i-ls%m]=ss[i];
for(int i=0;i<ls%m;i++)
st[m-ls%m+i]=ss[i];
for(int i=0;i<m;i++)
ss[i]=st[i];
}
struct ACAM{
int tr[N][2],ch[N][2],idx,c[N],fil[N],tg[N];
void insert(int l,int r)
{
int u=0;
for(int i=l;i<r;i++)
{
if(!ch[u][str[i]-'0'])
ch[u][str[i]-'0']=++idx;
u=ch[u][str[i]-'0'];
}
tg[u]=1;
}
int find(int l,int r)
{
int u=0;
for(int i=l;i<r;i++)
{
if(!ch[u][str[i]-'0'])
return 0;
u=ch[u][str[i]-'0'];
}
return tg[u];
}
LL ask()
{
int u=0;
LL ans=0;
for(int i=0;ss[i];i++)
u=tr[u][ss[i]-'0'],ans+=c[u];
return ans;
}
void clr()
{
for(int i=0;i<=idx;i++)
ch[i][0]=ch[i][1]=fil[i]=tg[i]=c[i]=tr[i][0]=tr[i][1]=0;
idx=0;
}
void build()
{
memset(c,0,sizeof(c));
l=1,r=0;
for(int i=0;i<2;i++)
if(tr[0][i]=ch[0][i])
q[++r]=ch[0][i];
while(l<=r)
{
for(int i=0;i<2;i++)
{
if(ch[q[l]][i])
fil[q[++r]=tr[q[l]][i]=ch[q[l]][i]]=tr[fil[q[l]]][i];
else
tr[q[l]][i]=tr[fil[q[l]]][i];
}
++l;
}
for(int i=0;i<=idx;i++)
c[i]=tg[i];
for(int i=0;i<=idx;i++)
c[q[i]]+=c[fil[q[i]]];
}
}x,y;
int main()
{
scanf("%d",&t);
for(int T=1;T<=t;T++)
{
printf("Case #%d:\n",T);
x.clr(),y.clr();
scanf("%d",&n),m=ls=0;
const int B=2;
for(int i=1;i<=n;i++)
{
char op=getchar();
while(op^'+'&&op^'?')
op=getchar();
if(op=='+')
{
++m;
scanf("%s",str+ln[m-1]);
shift(str+ln[m-1]);
ln[m]=ln[m-1]+strlen(str+ln[m-1]);
if(m%B==0)
{
for(int i=m-B+1;i<=m;i++)
{
x.insert(ln[i-1],ln[i]);
x.build();
}
y.clr();
}
else
{
if(!x.find(ln[m-1],ln[m]))
{
y.insert(ln[m-1],ln[m]);
y.build();
}
}
}
else
{
scanf("%s",ss);
shift(ss);
printf("%lld\n",ls=x.ask()+y.ask());
}
}
}
}
HDU 4787 GRE Revenge的更多相关文章
- [HDU 4787] GRE Words Revenge (AC自动机)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4787 题目大意: 给你若干个单词,查询一篇文章里出现的单词数.. 就是被我水过去的...暴力重建AC自 ...
- ●HDU 4787 GRE Words Revenge
题链: http://acm.hdu.edu.cn/showproblem.php?pid=4787 题解: AC自动机(强制在线构造) 题目大意: 有两种操作, 一种为:+S,表示增加模式串S, 另 ...
- HDU 4787 GRE Words Revenge
Description Now Coach Pang is preparing for the Graduate Record Examinations as George did in 2011. ...
- hdu 4898 The Revenge of the Princess’ Knight
传送阵:http://acm.hdu.edu.cn/showproblem.php?pid=4898 题目大意:一个首尾相连的字符串,将其分为k个子串,使得最大的字串最小 将所有子串排序,输出第k小即 ...
- 综合(奇技淫巧):HDU 5118 GRE Words Once More!
GRE Words Once More! Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/O ...
- hdu 4117 -- GRE Words (AC自动机+线段树)
题目链接 problem Recently George is preparing for the Graduate Record Examinations (GRE for short). Obvi ...
- HDU 5118 GRE Words Once More!
题目链接:HDU-5118 题意:给定一个有向无环图,每条边有一个权值.标定一些特定节点为“特殊节点”.从节点1出发到某“特殊节点”结束的路径,称为一个“GRE单词”.单词由路径上的权值组成.给定一组 ...
- HDU - 5088: Revenge of Nim II (问是否存在子集的异或为0)
Nim is a mathematical game of strategy in which two players take turns removing objects from distinc ...
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
题意:给定一个环形字符串,让他把它分成k份,使得最大的字典序 最小. 思路:二分答案,首先很明显答案所有可能是 n*n种 排序可以先求出最长公共前缀,这样比较就只需要比较公共前缀的下一位就能比较出两 ...
- HDU 4898 The Revenge of the Princess’ Knight(后缀数组+二分+暴力)(2014 Multi-University Training Contest 4)
Problem Description There is an old country and the king fell in love with a devil. The devil always ...
随机推荐
- vue3 组合式 api 单文件组件写法
1 Vue3 组合式 API 的基本原理和理念 1.1 Vue3 中的 Composition API Vue3 中的 Composition API 是一种新的编写组件逻辑的方式,它提供了更好的代码 ...
- [ABC126E] 1 or 2
2023-01-07 题目 题目传送门 翻译 翻译 难度&重要性(1~10):2 题目来源 AtCoder 题目算法 并查集 解题思路 因为每张卡片上的数字只能是 \(1\) 或者 \(2\) ...
- 三维模型OSGB格式轻量化压缩必要性分析
三维模型OSGB格式轻量化压缩必要性分析 三维模型是计算机图形学和视觉效果等领域的重要应用之一.然而,由于三维模型通常包含大量的几何信息.纹理信息和其他元素,导致其占用的存储空间和计算资源非常巨大.为 ...
- DesignPattern-part2
title: "modern C++ DesignPattern-Part2" date: 2018-04-10T19:08:49+08:00 lastmod: 2018-04-1 ...
- [Mysql] 存储过程简单理解
什么是存储过程 简单的说, 就是一组SQL语句集, 功能强大, 可以实现一些比较复杂的逻辑功能. 其实就和编程语言的面向过程函数一样. ps: 存储过程与触发器类似, 但存储过程是主动调用, 触发器是 ...
- 各快 100 倍?4G、5G、6G 相差这么多吗
二狗子今天晚上有点 emo,为什么呢? 原来是二狗子心心念很久的一个手游上线了,二狗子兴冲冲地下载了 40 多分钟,终于下载完了游戏.结果打开游戏一看,发现游戏内部的更新写着预计 30 分钟完成更新. ...
- Solution -「洛谷 P7395」「CoE-I 2021C」弹珠游戏
Description Link. 游戏在 \(4\times4\) 的菱形棋盘上进行: 两名玩家轮流放置弹珠,可以在横向.纵向.\(45\) 度斜线.\(135\) 度斜线方向未放置弹珠的位置连续放 ...
- C++指针和地址偏移在HotSpot VM中的应用
在前面我们介绍过new运算符,这个操作实际上上包含了如下3个步骤: 调用operator new的标准库函数.此函数会分配一块内存空间以便函存储相应类型的实例. 调用相应类的构造函数 返回一个指向该对 ...
- 23年9月最新微信小程序 手机号授权 (uniapp+盛派SDK) 帮你踩坑
一.背景 微信小程序手机号授权接口,从23年8月开始实行付费验证. 文档地址:https://developers.weixin.qq.com/miniprogram/dev/framework/op ...
- ddddocr1.4.8失效的解决方法
1. 问题描述 from selenium import webdriver from time import sleep driver = webdriver.Chrome() driver.max ...