D. String
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a string s. Each pair of numbers l and r that fulfill the condition 1 ≤ l ≤ r ≤ |s|, correspond to a substring of the string s, starting in the position l and ending in the position r (inclusive).
Let's define the function of two strings F(x, y) like this. We'll find a list of such pairs of numbers for which the corresponding substrings of string x are equal to string y. Let's sort this list of pairs according to the pair's first number's increasing. The value of function F(x, y) equals the number of non-empty continuous sequences in the list.
For example: F(babbabbababbab, babb) = 6. The list of pairs is as follows:
(1, 4), (4, 7), (9, 12)
Its continuous sequences are:
(1, 4)
(4, 7)
(9, 12)
(1, 4), (4, 7)
(4, 7), (9, 12)
(1, 4), (4, 7), (9, 12)
Your task is to calculate for the given string s the sum F(s, x) for all x, that x belongs to the set of all substrings of a string s.
Input
The only line contains the given string s, consisting only of small Latin letters (1 ≤ |s| ≤ 105).
Output
Print the single number — the sought sum.
Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.
Examples
input
aaaa
output
20
input
abcdef
output
21
input
abacabadabacaba
output
188
Note
In the first sample the function values at x equal to "a", "aa", "aaa" and "aaaa" equal 10, 6, 3 and 1 correspondingly.
In the second sample for any satisfying x the function value is 1.
题意:给出一个字符串,对于字符串的每个子串S,计算子串出现次数(t),并将(t imes (t+1)/2) 加入答案。
题解:其实,本题的答案就是计算三元组 ((i,j,k)) 的数目,使得后缀 (i) 和后缀 (j) 的 (lcp(i,j)) 大于等于k,首先可以处理 (i=j) 的情况,此时对答案的贡献(假设字符串总长度为 (l) ) 就是 子串的数目(l imes (l+1)/2),当 (i != j) 时,将(height[i]) 看成宽度为1,高度为 (height[i]) 的小长条,也就是对于每个(i) 计算 (i) 之前能形成的合法矩形数目,就是和这个题一样了。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
#define dbg(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<endl;
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod=1000000007;
const int maxn=100;
/*
*suffix array
*倍增算法 O(n*logn)
*待排序数组长度为n,放在0~n-1中,在最后面补一个0
*build_sa( ,n+1,m+1); //注意是n+1,m是s数组中的最大值;
*getHeight(,n);
*例如:
*n = 8;
*num[] = { 1, 1, 2, 1, 1, 1, 1, 2, $ };注意num最后一位为0,其他大于0
*rank[] = { 4, 6, 8, 1, 2, 3, 5, 7, 0 };rank[0~n-1]为有效值,rank[n]必定为0无效值
*sa[] = { 8, 3, 4, 5, 0, 6, 1, 7, 2 };sa[1~n]为有效值,sa[0]必定为n是无效值
*height[]= { 0, 0, 3, 2, 3, 1, 2, 0, 1 };height[2~n]为有效值
*
*/
int sa[maxn];//SA数组,表示将S的n个后缀从小到大排序后把排好序的
//的后缀的开头位置顺次放入SA中
int t1[maxn],t2[maxn],c[maxn];//求SA数组需要的中间变量,不需要赋值
int rk[maxn],height[maxn];
//待排序的字符串放在s数组中,从s[0]到s[n-1],长度为n,且最大值小于m,
//除s[n-1]外的所有s[i]都大于0,r[n-1]=0
//函数结束以后结果放在sa数组中
void build_sa(int s[],int n,int m)
{
int i,j,p,*x=t1,*y=t2;
//第一轮基数排序,如果s的最大值很大,可改为快速排序
for(i=0;i<m;i++)c[i]=0;
for(i=0;i<n;i++)c[x[i]=s[i]]++;
for(i=1;i<m;i++)c[i]+=c[i-1];
for(i=n-1;i>=0;i--)sa[--c[x[i]]]=i;
for(j=1;j<=n;j<<=1)
{
p=0;
//直接利用sa数组排序第二关键字
for(i=n-j;i<n;i++)y[p++]=i;//后面的j个数第二关键字为空的最小
for(i=0;i<n;i++)if(sa[i]>=j)y[p++]=sa[i]-j;
//这样数组y保存的就是按照第二关键字排序的结果
//基数排序第一关键字
for(i=0;i<m;i++)c[i]=0;
for(i=0;i<n;i++)c[x[y[i]]]++;
for(i=1;i<m;i++)c[i]+=c[i-1];
for(i=n-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i];
//根据sa和x数组计算新的x数组
swap(x,y);
p=1;x[sa[0]]=0;
for(i=1;i<n;i++)
x[sa[i]]=y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+j]==y[sa[i]+j]?p-1:p++;
if(p>=n)break;
m=p;//下次基数排序的最大值
}
}
void getHeight(int s[],int n)
{
int i,j,k=0;
for(i=0;i<=n;i++) rk[sa[i]]=i;
for(i=0;i<n;i++)
{
if(k)k--;
j=sa[rk[i]-1];
while(s[i+k]==s[j+k])k++;
height[rk[i]]=k;
}
}
char s[maxn];
int a[maxn];
int st[maxn],pre[maxn];
ll ans[maxn];
int main()
{
scanf("%s",s);
int l=(int)strlen(s);
rep(i,0,l) a[i]=s[i]-'a'+1;
a[l]=0;
build_sa(a,l+1,60);
getHeight(a,l);
ll res=1ll*l*(l+1)/2;
int top=0;
ans[1]=0;st[0]=1;
rep(i,2,l+1)
{
if(!height[i])
{
st[top]=i,ans[i]=0;
continue;
}
while(top&&height[st[top]]>=height[i]) top--;
ans[i]=ans[st[top]]+1ll*(i-st[top])*height[i];
st[++top]=i;
res+=ans[i];
}
printf("%lld
",res);
return 0;
}