Description
给定长度为
n
n
的字符串,
Ti
T
i
表示从第
i
i
个字符开始的后缀,求:
len(Ti) l e n ( T i ) 表示后缀 Ti T i 的长度, lcp(Ti,Tj) l c p ( T i , T j ) 表示 Ti,Tj T i , T j 最长公共前缀的长度。
Solution
感觉是BZOJ3879的弱化版。可能做题顺序有点问题。。
同样地搞出height
后用单调栈求左边、右边第一个height
比它小的的位置,然后直接搞就行了。
YMY神犇说可以建出SAM后树形DP求树上的点两两之间距离之和,真是太强啦!
Code
/************************************************
* Au: Hany01
* Date: Apr 8th, 2018
* Prob: [BZOJ3238][AHOI2013] 差异
* Email: hany01@foxmail.com
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read()
{
register int _, __; register char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 500005;
int n, sa[maxn], tp[maxn << 1], rk[maxn << 1], c[maxn], L[maxn], R[maxn], m, he[maxn], stk[maxn], top;
LL Ans;
char s[maxn << 1];
inline void RadixSort()
{
For(i, 0, m) c[i] = 0;
For(i, 1, n) ++ c[rk[i]];
For(i, 1, m) c[i] += c[i - 1];
Fordown(i, n, 1) sa[c[rk[tp[i]]] --] = tp[i];
}
inline void getSA()
{
For(i, 1, n) rk[i] = s[i], tp[i] = i;
m = 255, RadixSort();
for (register int k = 1, p; k <= n; k <<= 1)
{
p = 0;
For(i, n - k + 1, n) tp[++ p] = i;
For(i, 1, n) if (sa[i] > k) tp[++ p] = sa[i] - k;
RadixSort(), swap(tp, rk), rk[sa[1]] = 1, m = 1;
For(i, 2, n) rk[sa[i]] = tp[sa[i - 1]] == tp[sa[i]] && tp[sa[i - 1] + k] == tp[sa[i] + k] ? m : ++ m;
if (m >= n) return ;
}
}
inline void getHeight()
{
for (int i = 1, j, k = 0; i <= n; he[rk[i ++]] = k)
for (k = k ? k - 1 : 0, j = sa[rk[i] - 1]; s[i + k] == s[j + k]; ++ k) ;
}
int main()
{
#ifdef hany01
File("bzoj3238");
#endif
scanf("%s", s + 1), n = strlen(s + 1);
getSA(), getHeight();
Ans = (LL)(n - 1) * (LL)n * (LL)(n + 1) / 2ll;
he[1] = he[n + 1] = -1;
stk[top = 1] = 1;
For(i, 2, n) {
while (he[stk[top]] >= he[i]) -- top;
L[i] = stk[top] + 1, stk[++ top] = i;
}
stk[top = 1] = n + 1;
Fordown(i, n, 2) {
while (he[stk[top]] > he[i]) -- top;
R[i] = stk[top] - 1, stk[++ top] = i;
}
For(i, 2, n) Ans -= (LL)(i - L[i] + 1) * (LL)(R[i] - i + 1) * (LL)he[i] * 2ll;
printf("%lld\n", Ans);
return 0;
}
//镜湖三百里,菡萏发荷花。
// -- 李白《子夜吴歌·夏歌》