题面
Sol
求一个串不同子串的个数
每个子串一定是某个后缀的前缀,也就是求所有后缀不同前缀的个数
每来一个后缀\(suf(i)\)就会有,\(len-sa[i]+1\)的新的前缀,又由于有\(height\)个重复的,那么就是\(len-sa[i]+1-height\)的贡献
两个就只有数组大小的区别
# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1010);
int n, a[_], sa[_], rk[_], tmp[_], height[_], t[_], mx, T;
char s[_];
IL bool Cmp(RG int x, RG int y, RG int k){ return tmp[x] == tmp[y] && tmp[x + k] == tmp[y + k]; }
IL void Suffix_Sort(){
RG int m = mx;
for(RG int i = 0; i <= m; ++i) t[i] = 0;
for(RG int i = 1; i <= n; ++i) ++t[rk[i] = a[i]];
for(RG int i = 1; i <= m; ++i) t[i] += t[i - 1];
for(RG int i = n; i; --i) sa[t[rk[i]]--] = i;
for(RG int k = 1; k <= n; k <<= 1){
RG int l = 0;
for(RG int i = n - k + 1; i <= n; ++i) tmp[++l] = i;
for(RG int i = 1; i <= n; ++i) if(sa[i] > k) tmp[++l] = sa[i] - k;
for(RG int i = 0; i <= m; ++i) t[i] = 0;
for(RG int i = 1; i <= n; ++i) ++t[rk[tmp[i]]];
for(RG int i = 1; i <= m; ++i) t[i] += t[i - 1];
for(RG int i = n; i; --i) sa[t[rk[tmp[i]]]--] = tmp[i];
swap(rk, tmp); rk[sa[1]] = l = 1;
for(RG int i = 2; i <= n; ++i) rk[sa[i]] = Cmp(sa[i - 1], sa[i], k) ? l : ++l;
if(l >= n) break;
m = l;
}
for(RG int i = 1, h = 0; i <= n; ++i){
if(h) --h;
while(a[i + h] == a[sa[rk[i] - 1] + h]) ++h;
height[rk[i]] = h;
}
}
IL int Calc(){
RG int ans = n - sa[1] + 1;
for(RG int i = 2; i <= n; ++i) ans += n - sa[i] + 1 - height[i];
return ans;
}
int main(RG int argc, RG char* argv[]){
scanf("%d", &T);
while(T--){
scanf(" %s", s + 1);
n = strlen(s + 1); mx = 0;
for(RG int i = 1; i <= n; ++i) a[i] = s[i], mx = max(mx, a[i]);
Suffix_Sort();
printf("%d\n", Calc());
}
return 0;
}