题目中让我们求前缀出现的所有次数和。。。可以转化成rank【0】的后缀和其他所有后缀长度之和。。。
#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 100005
#define maxm 400005
#define eps 1e-10
#define mod 256
#define INF 1e17
#define lowbit(x) (x&(-x))
#define ls o<<1
#define rs o<<1 | 1
#define lson o->ch[0], L, mid
#define rson o->ch[1], mid+1, R
typedef long long LL;
//typedef int LL;
using namespace std;
char s[maxn];
int sa[maxn], t[maxn], t2[maxn], c[maxn];
int height[maxn], rank[maxn];
/*
void init(void)
{
cc[0] = 0;
for(int i = 1; i < maxn; i++)
cc[i] = (cc[i-1] + i) % mod;
}
*/
void build(int n, int m)
{
int i, *x = t, *y = t2, k, p;
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(k = 1; k <= n; k <<= 1) {
p = 0;
for(i = n-k; i < n; i++) y[p++] = i;
for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;
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];
swap(x, y), p = 1, x[sa[0]] = 0;
for(i = 1; i < n; i++)
x[sa[i]] = y[sa[i]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k] ? p-1 : p++;
if(p >= n) break;
m = p;
}
}
void getheight(int n)
{
int i, j, k = 0;
for(i = 0; i <= n; i++) rank[sa[i]] = i;
for(i = 0; i < n; i++) {
if(k) k--;
j = sa[rank[i]-1];
while(s[i+k] == s[j+k]) k++;
height[rank[i]] = k;
}
}
void work(int n)
{
int ans = 0, tmp, now;
now = rank[0];
tmp = height[now+1];
for(int i = now+1; i <= n; i++) {
tmp = min(tmp, height[i]);
ans = (ans + tmp) % mod;
}
tmp = height[now];
for(int i = now; i >= 1; i--) {
tmp = min(tmp, height[i]);
ans = (ans + tmp) % mod;
}
ans += n;
ans %= mod;
printf("%d\n", ans);
}
void debug(int n)
{
for(int i = 1; i <= n; i++)
printf("%d\n", height[i]);
}
int main(void)
{
int n;
while(scanf("%s", s)!=EOF) {
n = strlen(s);
build(n+1, 128);
getheight(n);
work(n);
}
return 0;
}