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. 【LaTeX】语法(更新中)

    目录 长度 空行 空格 超链接 数学公式 段落中(隐式) 单独成段(显式) 居中,左对齐,右对齐 居中 左对齐 右对齐 参考文献配置 TODO 参考资料 中文支持参考环境配置中的 内容,在这里不做重复 ...

  2. 如何将现有的`Blazor`项目的主题切换写的更好看?

    如何将现有的Blazor项目的主题切换写的更好看? 在现有的系统当中,我们的主题切换会比较生硬,下面我们将基于Masa Blazor实现好看的扩散主题切换的样式效果. 安装MASA.Template ...

  3. AI测试,给出的答案还挺那么回事儿的~

    今天文心一言全民开放了,所有人都可以正常下载使用了,不用像之前一样排队等号了.之前内测阶段倒也体验过,技术人员总是喜欢尝鲜,第一时间拿到邀请码后就各种调戏了TA一番,那时觉得给出的答案总有些差强人意, ...

  4. Java读取某个文件夹下的所有文件(支持多级文件夹)

    源码如下: package com.vocy.water.batch; import java.io.FileNotFoundException; import java.io.IOException ...

  5. 正则表达式快速入门三: python re module + regex 匹配示例

    使用 Python 实现不同的正则匹配(从literal character到 其他常见用例) reference python regular expression tutorial 目录 impo ...

  6. java线程的interrup、tUninterruptibles.sleepUninterruptibly和sleep、wait

    参考: (1)https://blog.csdn.net/qq_36031640/article/details/116696685 (2)https://blog.csdn.net/liuxiao7 ...

  7. Vue源码学习(七):合并生命周期(混入Vue.Mixin)

    好家伙,   1.使用场景 现在来,来想一下,作为一个使用Vue的开发者,假设现在我们要使用created(),我们会如何使用 1.1.  .vue文件中使用 <template> < ...

  8. std::copy与std::back_inserter引发的惨案

    #include <iostream> #include <vector> #include <numeric> #include <sstream> ...

  9. GPT-4 API waitlist

    Skip to main content Site Navigation GPT-4 API waitlist We're making GPT-4 available as an API for d ...

  10. Asp-Net-Core开发笔记:EFCore统一实体和属性命名风格

    前言 C# 编码规范中,类和属性都是大写驼峰命名风格(PascalCase / UpperCamelCase),而在数据库中我们往往使用小写蛇形命名(snake_case),在默认情况下,EFCore ...