Topic links: https://cn.vjudge.net/contest/318888#overview
The meaning of problems: Given a piano tone sequence P [Values range (1 to 88)], is now required to find a sequence satisfying
1, a length of at least 5
2, the sequence can transpose, i.e., the presence of two sub-sequences, one sub-sequence satisfies the plus / minus a number of other sequence can be
3, the sequence can not have two intersecting portion.
Meaning of the questions is simply looking for duplicate substring longest non-overlapping
Ideas:
In fact, this question may overlap and maximum repeat request longest substring like. We then find the longest base may overlap as long as the longest substring repeated again to determine what the sa [i] coincides with it so as not
1 #include <stdio.h> 2 #include <iostream> 3 #include <algorithm> 4 #include <string.h> 5 #include <stdlib.h> 6 #include <math.h> 7 #include <queue> 8 #include <set> 9 10 #define INF 0x3f3f3f3f 11 #define pii pair<int,int> 12 #define LL long long 13 using namespace std; 14 typedef unsigned long long ull; 15 const int maxn = 2e6+6; 16 17 int s[maxn]; 18 int sa[maxn],t[maxn],t2[maxn],c[maxn]; 19 int Rank[maxn],height[maxn]; 20 21 void build_sa(int n,int m) 22 { 23 int i,j,*x=t,*y=t2; 24 for (i=0;i<m;i++) 25 c[i] = 0; 26 for (i=0;i<n;i++) 27 c[x[i] = s[i]]++; 28 for (i=1;i<m;i++) 29 c[i] += c[i-1]; 30 for (i=n-1;i>=0;i--) 31 sa[--c[x[i]]] = i; 32 for (int k=1;k<=n;k<<=1) 33 { 34 int p = 0; 35 for (i=n-k;i<n;i++) 36 y[p++] = i; 37 for (i=0;i<n;i++) 38 { 39 if (sa[i]>=k) 40 y[p++] = sa[i]-k; 41 } 42 for (i=0;i<m;i++) 43 c[i] = 0; 44 for (i=0;i<n;i++) 45 c[x[y[i]]]++; 46 for (i=1;i<m;i++) 47 c[i] += c[i-1]; 48 for (i=n-1;i>=0;i--) 49 sa[--c[x[y[i]]]] = y[i]; 50 swap(x,y); 51 p = 1; 52 x[sa[0]] = 0; 53 for (i=1;i<n;i++) 54 x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++; 55 if (p>=n) 56 break; 57 m = p; 58 } 59 } 60 61 62 void getHeight(int n){ 63 int i,j,k=0; 64 for (i=1;i<=n;i++){ 65 Rank[sa[i]] = i; 66 } 67 for (i=0;i<n;i++){ 68 if (k) 69 k--; 70 j = sa[Rank[i]-1]; 71 while (s[i+k] == s[j+k]) 72 k++; 73 height[Rank[i]] = k; 74 } 75 } 76 77 int n; 78 bool check(int k){ 79 int maxsa=sa[0],minsa=sa[0]; 80 for (int i=1;i<n;i++){ 81 if (height[i]>=k){ 82 minsa = min(sa[i],minsa); 83 maxsa =max (in [i], maxsa); 84 } 85 else { 86 if (maxsa to time> = k) 87 return true ; 88 maxsa = a [i]; 89 minsa = in [i]; 90 } 91 } 92 if (maxsa to time> = k) { 93 return true ; 94 } 95 return false ; 96 } 97 98 int main () { 99 seconds (~scanf("%d",&n) && n!=0){ 100 for (int i=0;i<n;i++){ 101 scanf("%d",&s[i]); 102 } 103 for (int i=0;i<n;i++){ 104 if (i == n-1) 105 s[i] = 0; 106 else 107 s[i] = s[i+1]-s[i] + 100; 108 } 109 build_sa(n,200); 110 getHeight(n-1); 111 int l=0,r=n,mid; 112 while (l<=r){ 113 mid = (l+r)>>1; 114 if (check(mid)){ 115 l = mid+1; 116 } 117 else{ 118 r = mid-1; 119 } 120 } 121 if (r<4){ 122 printf("0\n"); 123 } 124 else{ 125 printf("%d\n",r+1); 126 } 127 } 128 return 0; 129 }