标签:while hose ati src check bre can enter rate
Description
Input
Output
Sample Input
yeshowmuchiloveyoumydearmotherreallyicannotbelieveit yeaphowmuchiloveyoumydearmother
Sample Output
27
1 /* 2 后缀数组就是套模板求先应得数组,这题用到了两个数组,分别是sa[],height[]; 3 sa[i]表示所有后缀按字典数排序后以s[i]开始的后缀排在第i位。height[i]表示 4 字典数为i和i-1后缀的的最长串的前缀。 5 */ 6 7 #include <iostream> 8 #include <stdio.h> 9 #include <string.h> 10 #include <algorithm> 11 using namespace std; 12 const int MAX = 2e5+10; 13 char str[MAX]; 14 int s[MAX], sa[MAX], t[MAX], t2[MAX], c[MAX], n; 15 int rank1[MAX], height[MAX]; 16 17 void build_sa(int m) { 18 int *x = t, *y = t2; 19 for(int i = 0; i < m; i ++) c[i] = 0; 20 for(int i = 0; i < n; i ++) c[x[i]=s[i]] ++; 21 for(int i = 1; i < m; i ++) c[i] += c[i-1]; 22 for(int i = n-1; i >= 0; i --) sa[--c[x[i]]] = i; 23 for(int k = 1; k <= n; k <<= 1) { 24 int p = 0; 25 for(int i = n-k; i < n; i ++) y[p++] = i; 26 for(int i = 0; i < n; i ++) if(sa[i] >= k) y[p++] = sa[i] - k; 27 for(int i = 0; i < m; i ++) c[i] = 0; 28 for(int i = 0; i < n; i ++) c[x[y[i]]]++; 29 for(int i = 1; i < m; i ++) c[i] += c[i-1]; 30 for(int i = n-1; i >= 0; i --) sa[--c[x[y[i]]]] = y[i]; 31 swap(x,y); 32 p = 1; 33 x[sa[0]] = 0; 34 for(int i = 1; i < n; i ++) 35 x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+k] == y[sa[i]+k]?p-1:p++; 36 if(p >= n)break; 37 m = p; 38 } 39 } 40 void getHeight() { 41 int k = 0; 42 for(int i = 0; i < n; i ++) rank1[sa[i]] = i; 43 for(int i = 0; i < n; i ++) { 44 if(k) k--; 45 int j = sa[rank1[i]-1]; 46 while(s[i+k] == s[j+k]) k++; 47 height[rank1[i]] = k; 48 } 49 } 50 int main() { 51 n = 0; 52 int l1, l2; 53 scanf("%s",str); 54 l1 = strlen(str); 55 for(int i = 0; i < l1; i ++) s[n++] = str[i] - ‘a‘ + 1; 56 s[n++] = 28; 57 scanf("%s",str); 58 l2 = strlen(str); 59 for(int i = 0; i < l2; i ++) s[n++] = str[i] - ‘a‘ + 1; 60 s[n++] = 0; 61 build_sa(30); 62 getHeight(); 63 int maxn = 0; 64 for(int i = 2; i < n; i ++) { 65 if(maxn < height[i]) { 66 if(sa[i] >= 0 && sa[i] < l1 && sa[i-1] > l1) 67 maxn = height[i]; 68 if(sa[i-1] >= 0 && sa[i-1] < l1 && sa[i] > l1) 69 maxn = height[i]; 70 } 71 } 72 printf("%d\n",maxn); 73 return 0; 74 }
标签:while hose ati src check bre can enter rate
原文地址:http://www.cnblogs.com/xingkongyihao/p/7257470.html