第一个后缀数组,拍模版……
/* * Author: stormdpzh * Created Time: 2012/7/22 14:22:35 * File Name: poj_2774.cpp */ #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <string> #include <cmath> #include <vector> #include <queue> #include <stack> #include <map> #include <set> #include <algorithm> #include <functional> #define sz(v) ((int)(v).size()) #define rep(i, n) for(int i = 0; i < n; i++) #define repf(i, a, b) for(int i = a; i <= b; i++) #define repd(i, a, b) for(int i = a; i >= b; i--) #define out(n) printf("%d\n", n) #define mset(a, b) memset(a, b, sizeof(a)) #define wh(n) while(1 == scanf("%d", &n)) #define whz(n) while(1 == scanf("%d", &n) && n != 0) #define lint long long using namespace std; const int MaxN = 200005; char s[MaxN]; int n; int sa[MaxN], height[MaxN], rank[MaxN], tmp[MaxN], top[MaxN]; void makesa() { int na = n < 256 ? 256 : n; mset(top, 0); rep(i, n) top[rank[i] = s[i] & 0xff]++; repf(i, 1, na - 1) top[i] += top[i - 1]; rep(i, n) sa[--top[rank[i]]] = i; for(int len = 1; len < n; len <<= 1) { int j; rep(i, n) { j = sa[i] - len; if(j < 0) j += n; tmp[top[rank[j]]++] = j; } sa[tmp[top[0] = 0]] = j = 0; repf(i, 1, n - 1) { if(rank[tmp[i]] != rank[tmp[i - 1]] || rank[tmp[i] + len] != rank[tmp[i - 1] + len]) top[++j] = i; sa[tmp[i]] = j; } memcpy(rank, sa, n * sizeof(int)); memcpy(sa, tmp, n * sizeof(int)); if(j >= n - 1) break; } } void lcp() { int i, j, k; for(j = rank[height[i = k = 0] = 0]; i < n - 1; i++, k++) while(k >= 0 && s[i] != s[sa[j - 1] + k]) { height[j] = (k--); j = rank[sa[j] + 1]; } } bool check(int i, int len) { if(sa[i] < len && sa[i - 1] < len) return false; if(sa[i] > len && sa[i - 1] > len) return false; return true; } int main() { while(1 == scanf("%s", s)) { int len = strlen(s); s[len] = 2; n = len; scanf("%s", s + n + 1); n = strlen(s); s[n] = 1; n++; makesa(); lcp(); int res = 0; rep(i, n) if(res < height[i] && check(i, len)) res = height[i]; printf("%d\n", res); } return 0; }