题意

给一个长度为\(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. week14课上测试

    说明 本次测试老师将所有课下测试的易错题全部重新考察了一遍,虽然是第二次做,还提前复习过,还是错了很多,回到寝室发现老师还没有结束测试,43分的我又忍不住再做了一遍. 做第二遍发现了有几个题目是蓝墨云 ...

  2. day 5 多态 类 静态

    1.多态 执行的时候才知道调用谁 class Dog(object): def print_self(self): print("大家好,我是来自西安的小白") class Xia ...

  3. 2762 helloparty·开车

    2762 helloparty·开车 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold   题目描述 Description hellokitty的一个朋友要来他家,但是 ...

  4. 打造linux下的source insight——vim插件安装使用总结

    source insight是windows下的优秀编辑器,适合阅读管理代码,主要有以下功能: 查找函数,变量或者宏的定义. 查找函数,变量或者宏的引用位置. 查找函数被调用的位置 查找某个符号在工程 ...

  5. html表单总结

    总结了下html表单: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  6. python Matplotlib数据可视化神器安装与基本应用

    Matplotlib Matplotlib 是一个非常强大的 Python 画图工具; 手中有很多数据, Matplotlib能帮你画出美丽的: 线图; 散点图; 等高线图; 条形图; 柱状图; 3D ...

  7. 2018百度之星开发者大赛-paddlepaddle学习

    前言 本次比赛赛题是进行人流密度的估计,因为之前看过很多人体姿态估计和目标检测的论文,隐约感觉到可以用到这次比赛上来,所以趁着现在时间比较多,赶紧报名参加了一下比赛,比赛规定用paddlepaddle ...

  8. 《Angular4从入门到实战》学习笔记

    <Angular4从入门到实战>学习笔记 腾讯课堂:米斯特吴 视频讲座 二〇一九年二月十三日星期三14时14分 What Is Angular?(简介) 前端最流行的主流JavaScrip ...

  9. oracle数据库之组函数

    组函数也叫聚合函数,用来对一组值进行运算,并且可以返回单个值 常见的组函数: (1)count(*),count(列名)  统计行数:找到所有不为 null 的数据来统计行数 (2)avg(列名)  ...

  10. 排序(C语言实现)

    读数据结构与算法分析 插入排序 核心:利用的是从位置0到位置P都是已排序的 所以从位置1开始排序,如果当前位置不对,则和前面元素反复交换重新排序 实现 void InsertionSort(Eleme ...