后缀数组2倍增可解。
#include <cstdio>
#include <cstring>
#include <cstdlib> #define MAXN 1005
#define INF 0xfffff
#define MAXM 27 int wa[MAXN], wb[MAXN], ws[MAXN], wv[MAXN];
char s[MAXN];
int str[MAXN], sa[MAXN];
int height[MAXN], rank[MAXN]; int max(int a, int b) {
return a>b ? a:b;
} int min(int a, int b) {
return a<b ? a:b;
} int cmp(int *r, int a, int b, int l) {
return r[a]==r[b] && r[a+l]==r[b+l];
} void da(int *r, int *sa, int n, int m) {
int i, j, *x = wa, *y = wb, *t;
int p; for (i=; i<m; ++i) ws[i] = ;
for (i=; i<n; ++i) ws[x[i]=r[i]]++;
for (i=; i<m; ++i) ws[i] += ws[i-];
for (i=n-; i>=; --i) sa[--ws[x[i]]] = i;
for (j=, p=; j<n; j*=, m=p) {
for (p=, i=n-j; i<n; ++i) y[p++] = i;
for (i=; i<n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j;
for (i=; i<n; ++i) wv[i] = x[y[i]];
for (i=; i<m; ++i) ws[i] = ;
for (i=; i<n; ++i) ws[wv[i]]++;
for (i=; i<m; ++i) ws[i] += ws[i-];
for (i=n-; i>=; --i) sa[--ws[wv[i]]] = y[i];
for (t=x, x=y, y=t, p=, x[sa[]]=, i=; i<n; ++i)
x[sa[i]] = cmp(y, sa[i-], sa[i], j) ? p- : p++;
}
} void calheight(int *r, int *sa, int n) {
int i, j, k = ; for (i=; i<=n; ++i) rank[sa[i]] = i;
for (i=; i<n; height[rank[i++]]=k)
for (k? --k:, j=sa[rank[i]-]; r[i+k]==r[j+k]; ++k)
/*do nothing*/;
} int getRepeat(int len, int n) {
int i, maxx, minn;
int ret = ; maxx = -;
minn = INF; for (i=; i<=n; ++i) {
if (height[i] >= len) {
maxx = max(sa[i], max(sa[i-], maxx));
minn = min(sa[i], min(sa[i-], minn));
} else {
if (maxx!=- && minn!=INF && (maxx-minn)>=len)
++ret;
maxx = -;
minn = INF;
}
} if (maxx!=- && minn!=INF && (maxx-minn)>=len)
++ret; return ret;
} void printRank(int n) {
int i; printf("print rank...\n");
for (i=; i<=n; ++i)
printf("%d ", rank[i]);
printf("\n");
} void printHeight(int n) {
int i; printf("print height...\n");
for (i=; i<=n; ++i)
printf("%d ", height[i]);
printf("\n");
} void printSa(int n) {
int i; printf("print sa...\n");
for (i=; i<=n; ++i)
printf("%d ", sa[i]);
printf("\n");
} int main() {
int i, len;
int ans; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif while (scanf("%s",s)!=EOF && (s[]!='#')) {
//printf("%s\n", s);
for (i=; s[i]; ++i)
str[i] = s[i] - 'a' + ;
str[i] = ;
len = i;
da(str, sa, len+, MAXM);
calheight(str, sa, len);
#ifndef ONLINE_JUDGE
printSa(len);
printRank(len);
printHeight(len);
#endif
for (ans=, i=; i<=len/; ++i) {
ans += getRepeat(i, len);
}
printf("%d\n", ans);
} return ;
}