BZOJ 3238 [Ahoi2013]差异(后缀自动机)
【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=3238
【题目大意】
给出一个串,设T[i]表示从第i位开始的后缀,
求sum(len(T[i])+len(T[j])-2*lcp(T[i],T[j]))
【题解】
根据反串的后缀自动机建立后缀树,
则两点的LCA在自动机中的length就是他们的LCP,
树形DP统计一下即可。
【代码】
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int N=1000010;
struct SAM{
char s[N];
int p,q,np,nq,cnt,lst,a[N][26],l[N],f[N],size[N],tot;
int Tr(char c){return c-'a';}
SAM(){cnt=0;lst=++cnt;}
void extend(int c){
p=lst;np=lst=++cnt;l[np]=l[p]+1;size[np]=1;
while(!a[p][c]&&p)a[p][c]=np,p=f[p];
if(!p)f[np]=1;
else{
q=a[p][c];
if(l[p]+1==l[q])f[np]=q;
else{
nq=++cnt;l[nq]=l[p]+1;
memcpy(a[nq],a[q],sizeof(a[q]));
f[nq]=f[q]; f[np]=f[q]=nq;
while(a[p][c]==q)a[p][c]=nq,p=f[p];
}
}
}
vector<int> v[N];
void BuildTree(){
scanf("%s",s+1);
int len=strlen(s+1);
for(int i=len;i;i--)extend(Tr(s[i]));
for(int i=2;i<=cnt;i++)v[f[i]].push_back(i);
}
long long res;
void Dfs(int x,int fx){
for(int i=0;i<v[x].size();i++){
int y=v[x][i];
Dfs(y,x); size[x]+=size[y];
}l[x]-=l[fx];
res=res-(long long)size[x]*(size[x]-1)*l[x];
}
void ShowResult(){
int len=strlen(s+1);
res=(long long)(len-1)*len*(len+1)/2;
for(int i=0;i<v[1].size();i++)Dfs(v[1][i],1);
printf("%lld\n",res);
}
}sam;
int main(){
sam.BuildTree();
sam.ShowResult();
return 0;
}
BZOJ 3238 [Ahoi2013]差异(后缀自动机)的更多相关文章
- BZOJ 3238: [Ahoi2013]差异 [后缀自动机]
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2512 Solved: 1140[Submit][Status ...
- BZOJ 3238 [Ahoi2013]差异 ——后缀自动机
后缀自动机的parent树就是反串的后缀树. 所以只需要反向构建出后缀树,就可以乱搞了. #include <cstdio> #include <cstring> #inclu ...
- BZOJ 3238: [Ahoi2013]差异 后缀自动机 树形dp
http://www.lydsy.com/JudgeOnline/problem.php?id=3238 就算是全局变量,也不要忘记,初始化(吐血). 长得一副lca样,没想到是个树形dp(小丫头还有 ...
- BZOJ.3238.[AHOI2013]差异(后缀自动机 树形DP/后缀数组 单调栈)
题目链接 \(Description\) \(Solution\) len(Ti)+len(Tj)可以直接算出来,每个小于n的长度会被计算n-1次. \[\sum_{i=1}^n\sum_{j=i+1 ...
- bzoj 3238: [Ahoi2013]差异 -- 后缀数组
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MB Description Input 一行,一个字符串S Output 一行,一个 ...
- BZOJ 3238: [Ahoi2013]差异 [后缀数组 单调栈]
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2326 Solved: 1054[Submit][Status ...
- 【BZOJ 3238】差异 后缀自动机+树形DP
题意 给定字符串,令$s_i$表示第$i$位开始的后缀,求$\sum_{1\le i < j \le n} len(s_i)+len(s_j)-2\times lcp(s_i,s_j)$ 先考虑 ...
- bzoj 3238 Ahoi2013 差异
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2357 Solved: 1067[Submit][Status ...
- ●BZOJ 3238 [Ahoi2013]差异
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3238 题解: 后缀数组套路深. 问题转化为求出任意两个后缀的LCP之和 在计算贡献时,各种不 ...
随机推荐
- vi的复制粘贴命令 -- (转)
vi编辑器有3种模式:命令模式.输入模式.末行模式.掌握这三种模式十分重要: 1.命令模式:vi启动后默认进入的是命令模式,从这个模式使用命令可以切换到另外两种模式,同时无论在任何模式下只要按一下[E ...
- PHP内存溢出 Allowed memory size of 解决办法
PHP出现如下错误:Allowed memory size of xxx bytes exhausted at xxx:xxx (tried to allocate xxx bytes) 关于 ...
- web项目打包后在代码中获取资源文件
在web项目里面,有时代码里面需要引用一些自定义的配置文件,这些配置文件如果放在类路径下,项目经过打包后使用的相对路径也会发生变化,所以以下给出了三种解决方案. 一.properties下配置 在类路 ...
- 03 Editor plugins and IDEs 编辑器插件和 ide
Editor plugins and IDEs 编辑器插件和 ide Introduction 介绍 Options 选项 Introduction 介绍 This document list ...
- python基础--random模块
python使用random生成随机数 下面是主要函数random.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0random.randint(a, b)生成的 ...
- caffe细节
1.BN层参数设置 在训练时所有BN层要设置use_global_stats: false(也可以不写,caffe默认是false) 在测试时所有BN层要设置use_global_stats: tru ...
- Hadoop案例(十)WordCount
WordCount案例 需求1:统计一堆文件中单词出现的个数(WordCount案例) 0)需求:在一堆给定的文本文件中统计输出每一个单词出现的总次数 1)数据准备:Hello.txt hello w ...
- day7异常处理
异常处理 下面看一个简单例子: data = {} try: data["name"] except KeyError as e: #e是错误的相信信息,错误的原因 print(& ...
- jquery最精简的全选反选功能
RT代码: function selallno(){ $('#form2 input[name=sel]:checkbox:not(:checked)').attr('checked',$('#for ...
- ASP.NET WebAPI 01-Demo
WebAPI作为构建RESTful的平台出来有段时间了,加上最近也在用,所以想把自己的心得记录下来.我就以一个简单的增删查改作为开篇. 准备 实体类(Figure)的定义. public class ...