POJ 2774 Long Long Message

POJ 2774 Long Long Message

Link

ideas

  Concatenate the two together, find the suffix array and height, and scan it once.

 

code

1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<iostream>
 5  
6  using  namespace std;
 7  
8  const  int N = 200100 ; 
 9  char s[N];
 10  int c[N ],t1[N],t2[N],sa[N],height[N],rnk[N];
 11  // sa[i] who is ranked i - subscript 
 12  // rnk[i] i's rank - rank 
 13  // height[i] The longest common prefix length of the suffix ranked i and the suffix ranked i-1 is 
14  int m = 130 ,n;
 15  
16 void get_sa() {
17     int *x = t1,*y = t2,p,i;
18     for (i=0; i<m; ++i) c[i] = 0; 
19     for (i=0; i<n; ++i) x[i] = s[i],c[x[i]]++;
20     for (i=1; i<m; ++i) c[i] += c[i-1];
21     for (i=n-1; i>=0; --i) sa[--c[x[i]]] = i;
22     for (int k=1; k<=n; k<<=1) {
23         p = 0;
24         for (i=n-k; i<n; ++i) y[p++] = i;
25         for (i=0; i<n; ++i) if (sa[i]>=k) y[p++] = sa[i]-k;
26         for (i=1; i<m; ++i) c[i] = 0;
27         for (i=0; i<n; ++i) c[ x[y[i]] ]++;
28         for (i=1; i<m; ++i) c[i] += c[i-1];
29         for (i=n-1; i>=0; --i) sa[--c[ x[y[i]] ]] = y[i];
30         swap(x,y);
31         p = 1;
32         x[sa[0]] = 0;
33         for (i=0; i<n; ++i) 
34             x[sa[i]] = y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
35         if (p >= n) break;
36         m = p;
37     }
38 }
39 void get_height() {
40     for (int i=0; i<n; ++i) rnk[sa[i]] = i;
41     int k = 0;
42     height[0] = 0;
43     for (int i=0; i<n; ++i) {
44         if (!rnk[i]) continue;
45         if (k) k--;
46         int j = sa[rnk[i]-1];
47         while (i+k<n && j+k<n && s[i+k]==s[j+k]) k++;
48         height[rnk[i]] = k;
49     }
50 }
51 
52 int main() {
53     scanf("%s",s);
54     int n1 = strlen(s);
55     scanf("%s",s+n1);
56     int n2 = strlen(s+n1);
57     n = n1 + n2;
58     get_sa();
59     get_height();
60     int ans = 0;
61     for (int i=1; i<n; ++i) {
62         int mn = min(sa[i],sa[i-1]);
63         int mx = max(sa[i],sa[i-1]);
64         if (mn < n1 && mx >= n1) ans = max(ans,height[i]);
65     }
66     printf("%d",ans);
67     return 0;
68 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325252813&siteId=291194637