题意

给一个长度为\(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. 20155338 2016-2017-2 《JAVA程序设计》课程总结

    ---恢复内容开始--- 20155338 2016-2017-2 <JAVA程序设计>课程总结 每周作业汇总: 预备作业一 预备作业二 预备作业三 <JAVA程序设计>第一周 ...

  2. WPF 带刻度的滑动条实现

    原文:WPF 带刻度的滑动条实现 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/BYH371256/article/details/83507170 ...

  3. easyui-dialog打开多次数据串台问题

    问题: 公司项目最近在用easyui写后台管理系统,发现dialog使用后总会报出莫名奇妙的问题 1.打开多次后数据显示上一次的,表单提交错误或者获取数据错误 2.来回跳转后,会有打开其它dialog ...

  4. cogs696 longest prefix

    cogs696 longest prefix 原题链接 IOI1996原题? 其实这题我不会. map+string+手动氧气大法好 //就是这么皮(滑稽 Code // It is made by ...

  5. ps 图层解锁后变成全格子(全透明)的解决方法

    其实是因为同时打开了好几个ps文件正在编辑中,所以解决方法就是重启ps,然后单独编辑一个文件,解锁后就不会再出现这种情况能,就能正常编辑了

  6. Android开发笔记——图片缓存、手势及OOM分析

    把图片缓存.手势及OOM三个主题放在一起,是因为在Android应用开发过程中,这三个问题经常是联系在一起的.首先,预览大图需要支持手势缩放,旋转,平移等操作:其次,图片在本地需要进行缓存,避免频繁访 ...

  7. abp core版本添加额外应用层

    1.新建类库WebProject.Application.App 2.添加WebProjectApplicationAppModule.cs 3.注册模块 using Abp.Application. ...

  8. nodejs express 加载html模板

    在nodejs中如使用express框架,她默认的是ejs和jade渲染模板.由于我在使用的时候觉得她的代码书写方式很不爽还是想用html的形式去书写,于是我找了使用了html模板. 直接上代码,主要 ...

  9. 239. [LeetCode ]Sliding Window Maximum

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  10. Qt类继承关系图

    分享两个资源,对于系统了解Qt框架的整体脉络很有帮助. Qt4类关系图+Qt5类关系图,PDF+JPG格式 [下载] Qt5类关系图(基于Qt5.1版),JPG格式[下载]