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的更多相关文章

  1. [HDU 4787] GRE Words Revenge (AC自动机)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4787 题目大意: 给你若干个单词,查询一篇文章里出现的单词数.. 就是被我水过去的...暴力重建AC自 ...

  2. ●HDU 4787 GRE Words Revenge

    题链: http://acm.hdu.edu.cn/showproblem.php?pid=4787 题解: AC自动机(强制在线构造) 题目大意: 有两种操作, 一种为:+S,表示增加模式串S, 另 ...

  3. HDU 4787 GRE Words Revenge

    Description Now Coach Pang is preparing for the Graduate Record Examinations as George did in 2011. ...

  4. hdu 4898 The Revenge of the Princess’ Knight

    传送阵:http://acm.hdu.edu.cn/showproblem.php?pid=4898 题目大意:一个首尾相连的字符串,将其分为k个子串,使得最大的字串最小 将所有子串排序,输出第k小即 ...

  5. 综合(奇技淫巧):HDU 5118 GRE Words Once More!

    GRE Words Once More! Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/O ...

  6. hdu 4117 -- GRE Words (AC自动机+线段树)

    题目链接 problem Recently George is preparing for the Graduate Record Examinations (GRE for short). Obvi ...

  7. HDU 5118 GRE Words Once More!

    题目链接:HDU-5118 题意:给定一个有向无环图,每条边有一个权值.标定一些特定节点为“特殊节点”.从节点1出发到某“特殊节点”结束的路径,称为一个“GRE单词”.单词由路径上的权值组成.给定一组 ...

  8. HDU - 5088: Revenge of Nim II (问是否存在子集的异或为0)

    Nim is a mathematical game of strategy in which two players take turns removing objects from distinc ...

  9. HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )

    题意:给定一个环形字符串,让他把它分成k份,使得最大的字典序 最小. 思路:二分答案,首先很明显答案所有可能是 n*n种  排序可以先求出最长公共前缀,这样比较就只需要比较公共前缀的下一位就能比较出两 ...

  10. 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 ...

随机推荐

  1. 在Jupyter中使用AI写代码,如有神助,太惊艳了

    昨晚看到一个可以在JupyterLab中使用的AI代码辅助工具jupyter-ai,它的交互确实非常棒,可以直接聊天,也可以就笔记中的代码提问,最出彩的是生成笔记功能,还是蛮惊艳的. 这里就极简介绍一 ...

  2. 《SQL与数据库基础》14. 存储过程 · 存储函数

    目录 存储过程 基本语法 变量 系统变量 用户定义变量 局部变量 if判断 参数 case判断 while循环 repeat循环 loop循环 游标 条件处理程序 存储函数 本文以 MySQL 为例 ...

  3. 《SQL与数据库基础》01. SQL概述 · 分类

    目录 SQL概述 SQL语法特征 SQL分类 本文以 MySQL 为例 SQL概述 SQL(Structured Query Language),结构化查询语言,用于访问和处理数据库的标准的计算机语言 ...

  4. 细数2019-2023年CWE TOP 25 数据,看软件缺陷的防护

    本文分享自华为云社区<从过去5年CWE TOP 25的数据看软件缺陷的防护>,作者:Uncle_Tom. "以史为鉴,可以知兴替".CWE 已经连续5年发布了 CWE ...

  5. Burp Suite抓包工具配置代理手机抓取数据包

    工作中很多手机上的问题因为环境差异导致无法在pc设备上完整的模拟真实物理手机,因此需要方法能抓取到手机设备上所有数据包发送详情.发现了这个好用的数据包抓取工具Burp Suite. 一.配置流程: 1 ...

  6. Mysql进击篇-存储引擎、索引、sql优化、视图、锁、innoDb、管理

    1.存储引擎 (1)连接层 最上层是一些客户端和连接服务,主要完成一些类似于连接处理,授权认证.以及相关的安全方案,服务器也会为安全接入的每个客户端验证它所具有的操作权限 (2)服务层 第二层架构主要 ...

  7. 什么是vfs以及它的作用

    VFS(Virtual File System,虚拟文件系统)是计算机操作系统中的一个概念,它提供了一个统一的抽象层,使得操作系统可以支持不同的文件系统类型和存储设备,而不需要直接与每个文件系统进行交 ...

  8. 分布式与微服务——Iaas,Paas和Saas、单体应用和缺点、微服务概念、传统 分布式 SOA 架构与微服务架构的区别、微服务实战、什么是RPC、CAP定理和BASE理论、唯一ID生成、实现分布式

    文章目录 1-什么是Iaas,Paas和Saas 一 IaaS基础设施服务 二 paas平台即服务 三saas软件即服务 四 总结 2-单体应用和缺点 一 单体应用 二 单体应用的缺陷 3-微服务概念 ...

  9. ESP32

    Main menu   Search Create account Log in Personal tools     Apply for a scholarship & submit you ...

  10. MySQL5.7版本单节点大数据量迁移到PXC8.0版本集群全记录-2

    本文主要记录57版本升级80版本的过程,供参考. ■ 57版本升级80版本注意事项 默认字符集由latin1变为utf8mb4 MyISAM系统表全部换成InnoDB表 sql_mode参数默认值变化 ...