D. String
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 E Nding in the position R (inclusive).
Let ' s define the function of the strings F(x,? Y) like this. We ' ll find a list of such pairs of numbers for which the corresponding substrings of string x is equal to St Ring 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 is:
- (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, which 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.
%lld Specificator to read or write 64-bit integers inс++. It is preferred to use the CIN, cout streams or the%i64d specificator.
Examplesinputcopy
Aaaa
Outputcopy
20
Inputcopy
ABCdef
Outputcopy
21st
Inputcopy
Abacabadabacaba
Outputcopy
188
Note
In the first sample of the function values at x equal to "a", "AA", "AAA" and "AAAA" equal, 6, 3 and 1 corres pondingly.
In the second, sample for any satisfying x , the function value is 1.
Test instructions: If a seed string s appears in the original string k times, according to the function defined by the topic, it produces the contribution is (K+1) *K/2
The condition is very strange, we try to transform the model, we will find that this function is equivalent to We put the K-s string into a row, each
String and its own and the following string match once, the total number of times is the function of the title requirement
So we can have a suffix array + height array, a maximum common prefix for each suffix, and a trailing suffix, and then count the answers based on the length
This thing can be done with a monotonous stack, and finally each suffix and self can be matched once, that is, if the read-in string length is n,ans+= (n+1) *N/2
Code:
1 //#include "bits/stdc++.h"2#include"Cstdio"3#include"Map"4#include"Set"5#include"Cmath"6#include"Queue"7#include"Vector"8#include"string"9#include"CTime"Ten#include"Stack" One#include"deque" A#include"Cstdlib" -#include"CString" -#include"iostream" the#include"algorithm" - - #defineDB Double - #definell Long Long + #defineVEC vector<ll> - #defineMt vector<vec> + #defineCI (x) scanf ("%d", &x) A #defineCD (x) scanf ("%lf", &x) at #defineCL (x) scanf ("%lld", &x) - #definePi (x) printf ("%d\n", X) - #definePD (x) printf ("%f\n", X) - #definePL (x) printf ("%lld\n", X) - //#define REP (i, X, y) for (int i=x;i<y;i++) - #defineRep (i, n) for (int i=0;i<n;i++) in using namespacestd; - Const intN = 1e6 +5; to Const intMoD = 1e9 +7; + Const intMoD = mod-1; - Const intINF =0x3f3f3f3f; the ConstDB PI = ACOs (-1.0); * ConstDB EPS = 1e-Ten; $ intSa[n];Panax Notoginseng intRk[n]; - intTmp[n]; the intLcp[n]; + intn,k; A BOOLcmpintIintj) { the if(Rk[i]! = Rk[j])returnrk[i]<Rk[j]; + Else - { $ intri=i+k<=n?rk[i+k]:-1; $ intrj=j+k<=n?rk[j+k]:-1; - returnri<RJ; - } the } - voidBulidstringSint*sa)Wuyi { theN= (int) s.size (); - for(intI=0; i<=n;i++){ Wusa[i]=i; -rk[i]=i<n?s[i]:-1; About } $ for(k=1; k<=n;k*=2){ -Sort (sa,sa+n+1, CMP); -tmp[sa[0]]=0; - for(intI=1; i<=n;i++){ Atmp[sa[i]]=tmp[sa[i-1]]+ (CMP (sa[i-1],sa[i])?1:0); + } the for(intI=0; i<=n;i++){ -rk[i]=Tmp[i]; $ } the } the } the voidLCP (stringSint*sa,int*LCP) { theN= (int) s.size (); - for(intI=0; i<=n;i++) rk[sa[i]]=i; in intH=0; thelcp[0]=0; the for(intI=0; i<n;i++){ About intj=sa[rk[i]-1]; the for(H. h--:0; J + H < N&&i + H < n&&s[j + h] = = S[i + h]; h++); thelcp[rk[i]-1] =h; the } + } - #defineX First the #defineY SecondBayi #definePair pair<int,int> the #defineMP Make_pair the -Stack<pair>STA; - intMain () the { the strings; theCin>>s; then=s.length (); - bulid (S,SA); the LCP (S,SA,LCP); thell ans= (LL) n (LL) (n+1)/2; thell cnt=0;94 for(intI=0; i<=n;i++) the { thePair ins=mp (Lcp[i],1);//Contribution to Lcp[i]*num the while(!sta.empty () && sta.top () .x>ins.x)98 { Aboutcnt-= (LL) sta.top (). x*sta.top (). Y; -ins.y+=sta.top (). Y;101 Sta.pop ();102 }103cnt+= (LL) ins.x*Ins.y;104 sta.push (INS); theans+=CNT;106 }107cout<<ans<<Endl;108 return 0;109}
Codeforces #123D: suffix array + monotonic stack