BZOJ 3238 【AHOI2013】 差异
题目链接:差异
写题时发现这道题当初已经用后缀数组写过了……但是既然学了后缀自动机那就再写一遍吧……
观察一下题目所给的式子:\[\sum_{1\leqslant i < j \leqslant n}len(T_i)+len(T_j)-2lcp(T_i,T_j)\]
可以发现前面两项其实是可以在\(O(n)\)的时间内算出来的。于是我们就只需要考虑后面那一项怎么算。
题目要求的是所有后缀的最长公共前缀之和,那么把串反一下就变成了所有前缀的最长公共后缀之和。
那么,我们构出翻转后的串的\(parent\)树(其实就是原串的后缀树),两个串的公共后缀就是他们在\(parent\)树上的\(lca\)节点包含的最长子串长度。
然后我们就可以枚举每个节点作为\(lca\),然后统计一下它的两两子树\(right\)集合大小之积即可。
下面贴代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
#define maxn 1000010 using namespace std;
typedef long long llg; char s[maxn>>1];
int n,tot,la,head[maxn],next[maxn],to[maxn],tt;
int son[maxn][26],len[maxn],fa[maxn],siz[maxn];
llg ans; void link(int x,int y){to[++tt]=y;next[tt]=head[x];head[x]=tt;}
void insert(int p,int x){
int np=++tot; siz[np]=1; len[np]=len[p]+1;
for(;p && !son[p][x];p=fa[p]) son[p][x]=np;
if(!p) fa[np]=1;
else{
int q=son[p][x];
if(len[q]==len[p]+1) fa[np]=q;
else{
int nq=++tot;
memcpy(son[nq],son[q],sizeof(son[q]));
fa[nq]=fa[q]; fa[np]=fa[q]=nq; len[nq]=len[p]+1;
for(;son[p][x]==q;p=fa[p]) son[p][x]=nq;
}
}
la=np;
} void dfs(int u){
for(int i=head[u],v;v=to[i],i;i=next[i]){
dfs(v);
ans-=(llg)siz[u]*siz[v]*len[u],siz[u]+=siz[v];
}
} int main(){
File("a");
scanf("%s",s+1); n=strlen(s+1); tot=la=1;
for(int i=n>>1;i;i--) swap(s[i],s[n-i+1]);
for(int i=1;i<=n;i++) insert(la,s[i]-'a');
for(int i=2;i<=tot;i++) link(fa[i],i);
dfs(1); ans<<=1; ans+=((llg)(n+1)*n*(n-1))>>1;
printf("%lld\n",ans);
return 0;
}
BZOJ 3238 【AHOI2013】 差异的更多相关文章
- BZOJ 3238: [Ahoi2013]差异 [后缀数组 单调栈]
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2326 Solved: 1054[Submit][Status ...
- bzoj 3238 Ahoi2013 差异
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2357 Solved: 1067[Submit][Status ...
- BZOJ 3238: [Ahoi2013]差异 [后缀自动机]
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2512 Solved: 1140[Submit][Status ...
- bzoj 3238: [Ahoi2013]差异 -- 后缀数组
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MB Description Input 一行,一个字符串S Output 一行,一个 ...
- ●BZOJ 3238 [Ahoi2013]差异
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3238 题解: 后缀数组套路深. 问题转化为求出任意两个后缀的LCP之和 在计算贡献时,各种不 ...
- 洛谷 P4248: bzoj 3238: [AHOI2013]差异
题目传送门:洛谷 P4248. 题意简述: 定义两个字符串 \(S\) 和 \(T\) 的差异 \(\operatorname{diff}(S,T)\) 为这两个串的长度之和减去两倍的这两个串的最长公 ...
- BZOJ 3238 [Ahoi2013]差异(后缀自动机)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3238 [题目大意] 给出一个串,设T[i]表示从第i位开始的后缀, 求sum(len( ...
- BZOJ 3238: [Ahoi2013]差异 后缀自动机 树形dp
http://www.lydsy.com/JudgeOnline/problem.php?id=3238 就算是全局变量,也不要忘记,初始化(吐血). 长得一副lca样,没想到是个树形dp(小丫头还有 ...
- BZOJ 3238: [Ahoi2013]差异((单调栈+后缀数组)/(后缀树))
[传送门[(https://www.lydsy.com/JudgeOnline/problem.php?id=3238) 解题思路 首先原式可以把\(len\)那部分直接算出来,然后通过后缀数组求\( ...
- BZOJ.3238.[AHOI2013]差异(后缀自动机 树形DP/后缀数组 单调栈)
题目链接 \(Description\) \(Solution\) len(Ti)+len(Tj)可以直接算出来,每个小于n的长度会被计算n-1次. \[\sum_{i=1}^n\sum_{j=i+1 ...
随机推荐
- Linux系列-Xshell连接本地VMware安装的Linux虚拟机
一.安装VMwareWorkstation并安装RedHat虚拟机,这里安装步骤省略,网络的资料很多,大侠们不如百度或者谷歌一下,大把的资料. 二.打开本地电脑的“网络连接”,你会发现多出了2个网络适 ...
- 封装自己的getClass
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 徐州网络赛A-Hard To Prepare【dp】【位运算】【快速幂】
After Incident, a feast is usually held in Hakurei Shrine. This time Reimu asked Kokoro to deliver a ...
- java 中的& &&区别以及 C++ 中& &&的区别
java中的 & &&两个运算符 (1).&在java中称做: 逻辑与 判断boolean类型,只有两者都是true时,才会返回true 举例: boolean b1= ...
- 分区实践 A PRIMARY KEY must include all columns in the table's partitioning function
MySQL :: MySQL 8.0 Reference Manual :: 23 Partitioning https://dev.mysql.com/doc/refman/8.0/en/parti ...
- Block Towers---cf626c(二分)
题目链接:http://www.codeforces.com/contest/626/problem/C 题意是有一群小朋友在堆房子,现在有n个小孩每次可以放两个积木,m个小孩,每次可以放3个积木,最 ...
- Oil Skimming---hdu4185(最大匹配)
题目链接 题意:有一个地图.代表水#代表油每个单元格是10*10的,现有10*20的勺子可以提取出水上漂浮的油,问最多可以提取几勺的油: 每次提取的时候勺子放的位置都要是油,不然就被污染而没有价值了: ...
- 爬虫之selenium使用
详细使用链接: 点击链接 selenium介绍: selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质 ...
- PAT 1111 Online Map[Dijkstra][dfs]
1111 Online Map(30 分) Input our current position and a destination, an online map can recommend seve ...
- Deep Learning(5)
五.应用实例 1.计算机视觉. ImageNet Classification with Deep Convolutional Neural Networks, Alex Krizhevsky, Il ...