SWERC2015-I Text Processor
题意
给一个长度为\(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的更多相关文章
- 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 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- windows进程详解
1:系统必要进程system process 进程文件: [system process] or [system process]进程名称: Windows内存处理系统进程描述: Windows ...
- 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 ...
- 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中 ...
- [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 ...
- AS 注解处理器 APT Processor MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 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 ...
- 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 ...
随机推荐
- c++ 时间函数和结构化数据
time和localtime 数据结构概念 struct关键字 认识数据结构 自定义结构 例:获取当前系统日期和时间;(代码例子) 一.函数: time 函数time()返回的是当 ...
- 【LG4103】[HEOI2014]大工程
[LG4103][HEOI2014]大工程 题面 洛谷 题解 先建虚树,下面所有讨论均是在虚树上的. 对于第一问:直接统计所有树边对答案的贡献即可. 对于第\(2,3\)问:记\(f[x]\)表示在\ ...
- R的数据结构
R语言中的数据结构包括标量.向量.矩阵.数组.列表以及数据框 目录 1 向量 2 矩阵 3 数据框 1 向量 向量是用于存储单一数据类型(数值.字符.逻辑值)的一维数组,示例如下: a <- c ...
- 探寻ASP.NET MVC鲜为人知的奥秘(2):与Entity Framework配合,让异步贯穿始终
Why 在应用程序,尤其是互联网应用程序中,性能一直是很多大型网站的困扰,由于Web2.0时代的到来,人们更多的把应用程序从C/S结构迁移到B/S结构,这样会带来客户端轻量,部署.试试方便快捷等优势, ...
- Selenium自动化测试第二天(上)
如有任何学习问题,可以添加作者微信:lockingfree 目录 Selenium自动化测试基础 Selenium自动化测试第一天(上) Selenium自动化测试第一天(下) Selenium自动化 ...
- Linux 安装ActiveMQ(使用Mac远程访问)
阅读本文需要安装JDK 一 ActiveMQ简介 activemq是用java语言编写的一款开源消息总线 activemq是apache出品 activemq消息的传递有两种类型 一种是点对点: 即一 ...
- Consul 架构(译)
Consul 架构 此篇文章主要对consul的相关内部技术细节进行简要概述. »术语 代理 - 代理是指consul集群中运行的consul实例,通过执行 consul agent 命令来启动. 代 ...
- spring cloud 入门系列七:基于Git存储的分布式配置中心--Spring Cloud Config
我们前面接触到的spring cloud组件都是基于Netflix的组件进行实现的,这次我们来看下spring cloud 团队自己创建的一个全新项目:Spring Cloud Config.它用来为 ...
- day09,10 函数
一.函数 什么是函数 函数: 对代码块和功能的封装和定义 定义一个事情或者功能. 等到需要的时候直接去用就好了. 那么这里定义的东西就是一个函数. 语法: def 函数名(形参): 函数体 函数名(实 ...
- Python基础灬文件常用操作
文件常用操作 文件内建函数和方法 open() :打开文件 read():输入 readline():输入一行 seek():文件内移动 write():输出 close():关闭文件 写文件writ ...