【题目链接】 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]差异(后缀自动机)的更多相关文章

  1. BZOJ 3238: [Ahoi2013]差异 [后缀自动机]

    3238: [Ahoi2013]差异 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2512  Solved: 1140[Submit][Status ...

  2. BZOJ 3238 [Ahoi2013]差异 ——后缀自动机

    后缀自动机的parent树就是反串的后缀树. 所以只需要反向构建出后缀树,就可以乱搞了. #include <cstdio> #include <cstring> #inclu ...

  3. BZOJ 3238: [Ahoi2013]差异 后缀自动机 树形dp

    http://www.lydsy.com/JudgeOnline/problem.php?id=3238 就算是全局变量,也不要忘记,初始化(吐血). 长得一副lca样,没想到是个树形dp(小丫头还有 ...

  4. BZOJ.3238.[AHOI2013]差异(后缀自动机 树形DP/后缀数组 单调栈)

    题目链接 \(Description\) \(Solution\) len(Ti)+len(Tj)可以直接算出来,每个小于n的长度会被计算n-1次. \[\sum_{i=1}^n\sum_{j=i+1 ...

  5. bzoj 3238: [Ahoi2013]差异 -- 后缀数组

    3238: [Ahoi2013]差异 Time Limit: 20 Sec  Memory Limit: 512 MB Description Input 一行,一个字符串S Output 一行,一个 ...

  6. BZOJ 3238: [Ahoi2013]差异 [后缀数组 单调栈]

    3238: [Ahoi2013]差异 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2326  Solved: 1054[Submit][Status ...

  7. 【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)$ 先考虑 ...

  8. bzoj 3238 Ahoi2013 差异

    3238: [Ahoi2013]差异 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2357  Solved: 1067[Submit][Status ...

  9. ●BZOJ 3238 [Ahoi2013]差异

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3238 题解: 后缀数组套路深. 问题转化为求出任意两个后缀的LCP之和 在计算贡献时,各种不 ...

随机推荐

  1. 2016.6.17——Valid Parentheses

    Valid Parentheses 本题收获: 1.stack的使用 2.string和char的区别 题目: Given a string containing just the character ...

  2. 3 - django-template模板基本使用

    目录 1 Template 1.1 模板的基础使用 1.1.1 变量 1.1.2 注释标签 1.1.3 深度查询 1.1.4 内置变量过滤器filter 1.1.5 自定义过滤器之filter 1.1 ...

  3. Spiral Matrix I & II

    Spiral Matrix I Given an integer n, generate a square matrix filled with elements from 1 to n^2 in s ...

  4. SQL 存储过程分页

    CREATE PROC p_Team_GetTemaList @pageindex INT , @pagesize INT , @keywords VARCHAR(200) , --模糊查询 名称 标 ...

  5. dorado 的学习位置、控件使用方法查找、示例演示地址

    dorado的学习位置: http://wiki.bsdn.org/display/dorado7/Project+Home dorado的控件使用方法查找: http://dorado7.bsdn. ...

  6. Codeforces Round #505

    Codeforces Round #505 A. Doggo Recoloring 题目描述:给定一个字符串,每次选择一个在字符串里面出现至少两次的字符,然后将这种字符变成那一种指定的字符,问最终这个 ...

  7. openjudge-NOI 2.5基本算法之搜索 专题题解目录

    1.1700 八皇后问题 2.1756 八皇后 3.1789 算24

  8. Vue 实现loading进度条

    项目中遇到的,用vue实现下: <template> <div class="plLoading"> <div class="plLoadi ...

  9. 密码记录工具keepass保存密码

    https://www.cnblogs.com/wicub/p/5753005.html

  10. SQL 标量函数-----日期函数 day() 、month()、year()

    select day(createtime) from life_unite_product --取时间字段的天值 select month(createtime) from life_unite_p ...