题意

给一个长度为\(n\)的字符串\(s\),再给定一个\(w\),问对于所有的\(i\in [1,n-w+1]\),\(s[i..i+w-1]\)有多少个不同字串。\(n,w\le 10^5\)。

分析

统计不同子串个数的问题可以使用后缀树。一个字符串的后缀树的总边长就是这个字符串的不同子串个数。解决这个问题,一个显然的方法就是划窗法,即每次删掉第一个字符,加入最后一个字符,再统计后缀树上边长的变更即可。

很明显这个方法需要一个在线的后缀树构建算法,可以用Ukkonen来做。那么我们如何求出每次删掉哪个后缀呢?我们删掉的肯定是当前后缀树中最长的后缀,也就是最早添加进去的那个叶子节点。所以我们维护一个队列,每次新建叶子的时候就加入队列中(压缩边也需要),每次删除队头即可。

代码

调了很久啊……这个题一定要想好再写(所有题都应该想好再写),会有一些需要注意的地方。好好利用删除的一定是叶子节点这个性质。

我的方法需要压缩边,即如果一个点只有一条出边,那么把这个点压缩掉。根据压缩后缀树的性质可以得到,每次最多删除一条边,压缩一条边。直接讨论一下now节点是否在这条边上就好啦。

然后,开long long……子串计数问题一定要注意这个啊!!

#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long giant;
int read() {
int x=0,f=1;
char c=getchar();
for (;!isdigit(c);c=getchar()) if (c=='-') f=-1;
for (;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int maxn=2e5+10;
const int maxc=27;
giant ans=0,Ans[maxn];
int w,que[maxn],ql=1,qr=0;
struct ST {
const static int inf=1e8;
int t[maxn][maxc],father[maxn],sons[maxn],start[maxn],len[maxn],link[maxn],now,rem,s[maxn],n,leaf,tot,pool[maxn];
ST ():now(1),rem(0),n(0),leaf(0),tot(1) {
for (int i=1;i<maxn;++i) pool[i]=i;
father[1]=0;
len[0]=inf;
}
void erase(int &x) {
memset(t[x],0,sizeof t[x]),start[x]=len[x]=link[x]=father[x]=sons[x]=0;
pool[tot--]=x;
x=0;
}
int node(int star,int le) {
int nw=pool[++tot];
start[nw]=star,len[nw]=le,link[nw]=1;
return nw;
}
void add(int c) { // c is a char
s[++n]=c,++rem;
for (int last=1;rem;) {
while (rem>len[t[now][s[n-rem+1]]]) rem-=len[now=t[now][s[n-rem+1]]];
int ed=s[n-rem+1];
int &v=t[now][ed];
int x=s[start[v]+rem-1];
if (!v) {
father[que[++qr]=v=node(n-rem+1,inf)]=now;
++sons[now];
link[last]=now;
last=now;
} else if (x==c) {
link[last]=now;
last=now;
break;
} else {
int u=node(start[v],rem-1);
father[u]=father[v];
father[que[++qr]=t[u][c]=node(n,inf)]=u;
father[t[u][x]=v]=u,start[v]+=rem-1;
if (len[v]<inf) len[v]-=rem-1;
sons[link[last]=v=u]=2;
last=v;
}
++leaf;
if (now==1) --rem; else now=link[now];
}
}
void compress(int x) {
if (!father[x]) return;
if (sons[x] && (--sons[x])!=1) return;
int i;
for (i=1;i<maxc;++i) if (t[x][i]) break;
int u=t[x][i];
start[u]-=len[x];
if (len[u]<inf) len[u]+=len[x];
father[u]=father[x];
erase(t[father[x]][s[start[x]]]);
t[father[u]][s[start[u]]]=u;
}
void del(int x) { // x is a point
int f=father[x];
if (now==f) {
if (!rem || t[now][s[n-rem+1]]!=x) {
ans-=n-start[x]+1;
--leaf;
erase(t[f][s[start[x]]]);
if (now!=1) now=father[now],rem+=len[f];
compress(f);
return;
}
ans-=n-rem-start[x]+1;
start[x]=n-rem+1;
que[++qr]=x;
if (now==1) --rem; else now=link[now];
} else {
ans-=n-start[x]+1;
erase(t[f][s[start[x]]]);
compress(f);
--leaf;
}
}
void run() {
for (int i=1;i<=tot;++i) if (pool[i]!=1) ans+=min(len[pool[i]],n-start[pool[i]]+1);
}
} sut;
int main() {
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
freopen("my.out","w",stdout);
#endif
int n,m;
static char s[maxn];
scanf("%s",s+1);
n=strlen(s+1);
m=read(),w=read();
for (int i=1;i<=w;++i)
sut.add(s[i]-'a'+1);
sut.run();
Ans[1]=ans;
for (int i=w+1;i<=n;++i) {
if (ql<=qr) sut.del(que[ql++]);
sut.add(s[i]-'a'+1);
ans+=sut.leaf;
Ans[i-w+1]=ans;
}
while (m--) printf("%lld\n",Ans[read()]);
return 0;
}

SWERC2015-I Text Processor的更多相关文章

  1. How To Use the AWK language to Manipulate Text in Linux

    https://www.digitalocean.com/community/tutorials/how-to-use-the-awk-language-to-manipulate-text-in-l ...

  2. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  3. windows进程详解

    1:系统必要进程system process    进程文件: [system process] or [system process]进程名称: Windows内存处理系统进程描述: Windows ...

  4. Guide to make CentOS 7 the perfect desktop

    原文地址:  http://www.dedoimedo.com/computers/fedora-pimp.html My original review of CentOS 7 was less e ...

  5. ti processor sdk linux am335x evm /bin/create-sdcard.sh hacking

    #!/bin/bash # # ti processor sdk linux am335x evm /bin/create-sdcard.sh hacking # 说明: # 本文主要对TI的sdk中 ...

  6. [r]Seven habits of effective text editing

    Seven habits of effective text editing(via) Bram Moolenaar November 2000 If you spend a lot of time ...

  7. AS 注解处理器 APT Processor MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  8. Separate code and data contexts: an architectural approach to virtual text sharing

    The present invention provides a processor including a core unit for processing requests from at lea ...

  9. Flexible implementation of a system management mode (SMM) in a processor

    A system management mode (SMM) of operating a processor includes only a basic set of hardwired hooks ...

随机推荐

  1. 20155325实验四 Android程序设计

    实验四 Android程序设计-1 Android Stuidio的安装测试: 参考<Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)&g ...

  2. 我与虚拟机的初次接触及初探Liux命令 20155338

    初识虚拟机及学习Linux命令的些许收获 虚拟机的安装 这个假期算是第一次正式的接触了虚拟机,以前在平时生活中也有听到过,但是真正自己动手安装虚拟机却是第一次,确实是既紧张又兴奋. 我是依据老师所发的 ...

  3. asp.net self host and urlacl(解决UnHandledException Message:拒绝访问的问题)

    命令提示符(管理员权限)需要添加的代码: netsh http add urlacl url=http://*:9999/ user=Everyone listen=yes 其中: url:代码中的u ...

  4. POJ2513_Colored Sticks_KEY

    题目传送门 题目大意:给你若干根木棍,每根木棍有前后两种颜色,连接两根木棍需要前后颜色相同,求能否将所有木棍连接在一起. Solution: 不要将木棍看成点,将颜色看成点. 其实就是求是否存在欧拉路 ...

  5. 手撕一个 Galgame 神器——Shub-Niggurath Project

    一.想法 Galgame 我们大概可以分为好用的 Galgame 和好玩的 Galgame,但是如果你把好玩的 Galgame 拿来用的话,有时候会十分让人着急.如果你躺在床上,一只手还在按压键盘实际 ...

  6. textbox的验证

    代码如下: textBox1.KeyDown += (a, b) => { if (b.KeyCode == Keys.Enter) { textBox2.Focus(); } }; textB ...

  7. Google TensorFlow for GPU安装、配置大坑

    Google TensorFlow for GPU安装.配置大坑 从本周一开始(12.05),共4天半的时间,终于折腾好Google TensorFlow for GPU版本,其间跳坑无数,摔得遍体鳞 ...

  8. Lua学习笔记(3):运算符

    算术运算符 运算符 描述 + 加法运算符 - 减法运算符 * 乘法运算符 / 除法运算符 % 取模运算符 ^ 乘幂 A=3 print(A^2)输出9 关系运算符 ~= 不等于 == 等于 > ...

  9. POJ 1417 并查集 dp

    After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ...

  10. python3 ,AttributeError: module 'tensorflow' has no attribute 'merge_summary'

    error:tensorflow有些方法属性被改了, self.summary_writer = tf.train.SummaryWriter(summary_dir)改为:summary.FileW ...