1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| #include <bits/stdc++.h> #define int long long using namespace std;
const int N = 666;
inline int read() { int s(0), w(1); char ch = getchar(); while (ch < '0' || ch > '9') {if (ch == '-') w = -1; ch = getchar();} while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar(); return s * w; }
inline void put(int x) { if (! x) putchar('0'); if (x < 0) putchar('-'), x = -x; int num(0); char c[66]; while (x) c[++ num] = x % 10 + 48, x /= 10; while (num) putchar(c[num --]); return (void)(putchar('\n')); }
int nex[N]; char ch[N], s[N];
signed main() { int i, j, l, n, len, res(0); scanf ("%s", ch + 1), n = strlen(ch + 1); for (l = 1; l <= n; ++ l) { j = len = 0; memset(nex, 0, sizeof nex), memset(s, 0, sizeof s); for (i = l; i <= n; ++ i) s[++ len] = ch[i]; for (i = 2; i <= len; ++ i) { while (j && s[j + 1] != s[i]) j = nex[j]; if (s[j + 1] == s[i]) ++ j; nex[i] = j; } for (i = 1; i <= len; ++ i) res = max(res, nex[i]); } put(res); return 0; }
|