1 #include <bits/stdc++.h>
2 using namespace std;
3 const int maxn = 1000010;
4 char s[maxn];
5 int maxlen[maxn<<1], minlen[maxn<<1], tr[maxn<<1][26], link[maxn<<1];
6 int sz;
7
8 int news(int maxl, int minl, int *trans, int lk){
9 maxlen[sz] = maxl;
10 minlen[sz] = minl;
11 for(int i = 0; i < 26; i++){
12 if(trans == NULL) tr[sz][i] = -1;
13 else tr[sz][i] = trans[i];
14 }
15 link[sz] = lk;
16 return sz++;
17 }
18 int add(char ch, int u){
19 int c = ch - 'a';
20 int z = news(maxlen[u] + 1, -1, NULL, -1);
21 int v = u;
22 while(~v && tr[v][c] == -1){
23 tr[v][c] = z;
24 v = link[v];
25 }
26 if(v == -1){
27 minlen[z] = 1;
28 link[z] = 0;
29 return z;
30 }
31 int x = tr[v][c];
32 if(maxlen[v] + 1 == maxlen[x]){
33 minlen[z] = maxlen[x] + 1;
34 link[z] = x;
35 return z;
36 }
37 int y = news(maxlen[v] + 1, -1, tr[x], link[x]);
38 minlen[x] = maxlen[y] + 1;
39 link[x] = y;
40 minlen[z] = maxlen[y] + 1;
41 link[z] = y;
42 int w = v;
43 while(~w && tr[w][c] == x){
44 tr[w][c] = y;
45 w = link[w];
46 }
47 minlen[y] = maxlen[link[y]] + 1;
48 return z;
49 }
50
51 int main(){
52 while(scanf("%s", s) != EOF){
53 int len = strlen(s);
54 sz = 0;
55 int pre = news(0, 0, NULL, -1);
56 for(int i = 0; i < len; i++){
57 pre = add(s[i], pre);
58 }
59 long long ans = 0;
60 for(int i = 1; i < sz; i++) ans += maxlen[i] - minlen[i] + 1;
61 printf("%lld
", ans);
62 }
63 }