【bzoj3238】[Ahoi2013]差异

Description

Input

一行,一个字符串S

Output

一行,一个整数,表示所求值

Sample Input

cacao

Sample Output

54

题解:

任意两个字符串的lcp是什么,就是如

a,b  那么若a==b 那么为len(a)

  否则设sa[a]<sa[b] 那么为min(height[sa[a]+1-------sa[b]])

 #include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio> #define N 500007
#define ll long long
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if (ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n;
int stk[N],f[N],g[N];
struct SA
{
char s[N];
int a[N],b[N],cnta[N],cntb[N],tsa[N],height[N],sa[N],rk[N*];
void Get_SA()
{
for (int i=;i<=;i++)cnta[i]=;
for (int i=;i<=n;i++)cnta[(int)s[i]]++;
for (int i=;i<=;i++)cnta[i]+=cnta[i-];
for (int i=n;i>=;i--)sa[cnta[(int)s[i]]--]=i;
rk[sa[]]=;
for (int i=;i<=n;i++)rk[sa[i]]=rk[sa[i-]]+(s[sa[i]]!=s[sa[i-]]);
for (int i=;rk[sa[n]]!=n;i<<=)
{
for (int j=;j<=n;j++)a[j]=rk[j],b[j]=rk[j+i];
for (int j=;j<=n;j++)cnta[j]=cntb[j]=;
for (int j=;j<=n;j++)cnta[a[j]]++,cntb[b[j]]++;
for (int j=;j<=n;j++)cnta[j]+=cnta[j-],cntb[j]+=cntb[j-];
for (int j=n;j>=;j--)tsa[cntb[b[j]]--]=j;
for (int j=n;j>=;j--)sa[cnta[a[tsa[j]]]--]=tsa[j];
rk[sa[]]=;
for (int j=;j<=n;j++)
rk[sa[j]]=rk[sa[j-]]+(a[sa[j]]!=a[sa[j-]]||b[sa[j]]!=b[sa[j-]]);
}
}
void Get_Height()
{
int len=;
for (int i=;i<=n;i++)
{
if (len)len--;
while(s[i+len]==s[sa[rk[i]-]+len])len++;
height[rk[i]]=len;
}
}
}S;
int main()
{
scanf("%s",S.s+);
n=strlen(S.s+);
ll ans=;
for (int i=;i<=n;i++)
{
ans+=(ll)(i-)*i;
ans+=(ll)i*(i-)/;
}
S.Get_SA();
S.Get_Height();
int tot=;
for (int i=;i<=n;i++)
{
while(tot>&&S.height[i]<S.height[stk[tot]])
f[stk[tot--]]=i-;
stk[++tot]=i;
}
while(tot)f[stk[tot--]]=n;
tot=;
for (int i=n;i>=;i--)
{
while(tot>&&S.height[i]<=S.height[stk[tot]])g[stk[tot--]]=i+;
stk[++tot]=i;
}
while(tot)g[stk[tot--]]=;
for (int i=;i<=n;i++)
ans-=(ll)S.height[i]*(ll)(f[i]-i+)*(ll)(i-g[i]+)*;
printf("%lld\n",ans);
}

bzoj3238 [Ahoi2013]差异 后缀数组+单调栈的更多相关文章

  1. 【BZOJ-3238】差异 后缀数组 + 单调栈

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

  2. 【BZOJ3238】[Ahoi2013]差异 后缀数组+单调栈

    [BZOJ3238][Ahoi2013]差异 Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao Sample Ou ...

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

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

  4. [AHOI2013] 差异 - 后缀数组,单调栈

    [AHOI2013] 差异 Description 求 \(\sum {len(T_i) + len(T_j) - 2 lcp(T_i,T_j)}\) 的值 其中 \(T_i (i = 1,2,... ...

  5. 【bzoj3238】差异[AHOI2013](后缀数组+单调栈)

    题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3238 这道题从大概半年以前就开始啃了,不过当时因为一些细节没调出来,看了Sakits神犇 ...

  6. [BZOJ3238][AHOI2013]差异(后缀数组)

    求和式的前两项可以直接算,问题是对于每对i,j计算LCP. 一个比较显然的性质是,LCP(i,j)是h[rk[i]+1~rk[j]]中的最小值. 从h的每个元素角度考虑,就是对每个h计算有多少对i,j ...

  7. BZOJ3238 [Ahoi2013]差异 【后缀数组 + 单调栈】

    题目链接 BZOJ3238 题解 简单题 经典后缀数组 + 单调栈套路,求所有后缀\(lcp\) #include<iostream> #include<cstdio> #in ...

  8. BZOJ_3238_[Ahoi2013]差异_后缀数组+单调栈

    BZOJ_3238_[Ahoi2013]差异_后缀数组+单调栈 Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao ...

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

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

随机推荐

  1. python_104_面向对象总结

    参考(都要认真看看):http://www.cnblogs.com/alex3714/articles/5188179.html http://www.cnblogs.com/alex3714/art ...

  2. Python2 和Python3 的区别

    print Python 2中的print语句被Python 3中的print()函数取代,这意味着在Python 3中必须用括号将需要输出的对象括起来. 在Python 2中使用额外的括号也是可以的 ...

  3. Python基础篇 -- 集合

    set集合 set 中的元素是不重复的,无序的 里面的元素必须是可hash的,(int str tuple bool) set 就是dict 类型的数据,但是不保存value 只保存 key set集 ...

  4. DateTime与long互转

    DateTime转long: public static long GetDateLong(object time) { DateTime epoc = TimeZone.CurrentTimeZon ...

  5. graphviz 绘制架构图

    架构图: 1.依赖调用关系.(类似文献引用关系, graphviz 自动将每一次调用升一次层级) 2.依赖调用可能是上下层级调用,也可能是同层级引用. 需人工去梳理出这些关系 3. 引用多的用颜色标识 ...

  6. octave-basic

    在coursera上斯坦福的machine learning,lecturer极力推荐开源的编程环境Octave入手,所以我也下载了来试一试吧== 参考链接:http://www.linuxdiyf. ...

  7. LeetCode(274)H-Index

    题目 Given an array of citations (each citation is a non-negative integer) of a researcher, write a fu ...

  8. POJ:1961-Period(寻找字符串循环节)

    Period Time Limit: 3000MS Memory Limit: 30000K Description For each prefix of a given string S with ...

  9. 第十四届华中科技大学程序设计竞赛决赛同步赛 Beautiful Land

    It’s universally acknowledged that there’re innumerable trees in the campus of HUST.Now HUST got a b ...

  10. MySQL-状态Waiting on empty queue引申

    MySQL 事件调度器示例演示 我们大家都知道MySQL 事件调度器是在 MySQL 5.1 中新生的一个较为特殊的功能,其可以作为定时任务调度器,来取代部分原先只能用操作系统任务调度器才能完成的定时 ...