typedeflonglong LL;typedefunsignedlonglong ULL;constint N =100010, P =13331;classSolution{public:
ULL h[N], p[N];
ULL get(int l,int r){return h[r]- h[l -1]* p[r - l +1];}// 原串的前缀[1, len] 是否与 s.substr(base, len) 相等boolcheck(int base,int len){returnget(1, len)==get(base, base + len -1);}
LL sumScores(string s){int n = s.size(); s =' '+ s;
p[0]=1;for(int i =1; i <= n; i ++){
h[i]= h[i -1]* P + s[i];
p[i]= p[i -1]* P;}
LL res =0;for(int i = n; i >=1; i --){int l =0, r = n - i +1;// 二分长度while(l < r){int mid = l + r +1>>1;if(check(i, mid)) l = mid;else r = mid -1;}
res += r;}return res;}};