标签:ica break math main continue highlight amp uil end
题意:
就是在一串数字中,找到连续的一串数字,出现两次的最大长度是多少,大于5输出5,小于5输出0。当然还有变调,就是可能同时大一或者小一。
分析:以后一个数和前一个数字的差成立数据,即可避免变调的情况。然后二分答案,后缀数组,求最长公共前缀height数组即可。
坑点:
在后面补一个0。
我用的是刘汝佳的板子,一般情况在后面补一个特别小的数字(比数组里内容都小的数字就可以了)
可是这次以差建立数组,会有0的情况,就改了改板子,(见第27,28行)
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<cmath> #include<string> #define LL long long using namespace std; const int INF = 0x3f3f3f3f; const int mod = 1e9 + 7; const int maxn = 3e4 + 5; const int maxs = 30; int s[maxn]; int sa[maxn], t[maxn], t2[maxn], c[maxn], n , m; int d[maxn][maxs]; int height[maxn], rank[maxn]; void build_sa(int m) { int i, *x = t, *y = t2; for ( i = 0; i < m; i++) c[i] = 0; for ( i = 0; i < n; i++) c[x[i] = s[i]]++; for ( i = 1; i < m; i++) c[i] += c[i - 1]; for (i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i; for (int k = 1; k <= n; k <<= 1){ int p = 0; // for (i = n - k; i < n; i++) for (i = 0; i < n; i++) { if (sa[i] >= k) y[p++] = sa[i] - k; } for (i = 0; i < m; i++) c[i] = 0; for (i = 0; i < n; i++) c[x[y[i]]]++; for (i = 0; i < m; i++) c[i] += c[i - 1]; for (i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i]; swap(x, y); p = 1; x[sa[0]] = 0; for (i = 1; i < n; i++){ x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k] ? p - 1 : p++; } if (p >= n) break; m = p; } } void getHeight() { int i, j, k = 0; for (i = 0; i < n; i++) rank[sa[i]] = i; for (i = 0; i < n; i++){ if (k) k--; j = sa[rank[i] - 1]; while (i+k<n&&j+k<n&&s[i + k] == s[j + k]) k++; height[rank[i]] = k; } } void RMQ_init() { for(int i = 0 ; i<n; i++) d[i][0] = sa[i]; for(int j = 1 ; (1<<j) - 1 <=n ; j++){ for(int i = 0 ; i + (1<<j) - 1 < n ; i++){ d[i][j] = min(d[i][j-1] , d[i + (1<< (j - 1))][ j -1 ]); } } } int RMQ(int L , int R) { int k = 0; while((1<<(k + 1)) <= R - L + 1) k++; return min(d[L][k] , d[R - (1 << k) + 1][k]); } int q[maxn]; bool check(int mid){ int minsa,maxsa; maxsa=minsa=sa[0]; for (int i=1;i<n;i++){ if (height[i]<mid){ maxsa=minsa=sa[i]; }else{ maxsa=max(maxsa,sa[i]); minsa=min(minsa,sa[i]); if (maxsa-minsa>mid){ return 1; } } } return 0; } int main() { while(scanf("%d",&n)!=EOF&&n){ for (int i=0;i<n;i++) scanf("%d",&q[i]); if (n<=1){ printf("0\n"); continue; } for (int i=0;i<n-1;i++){ s[i]=q[i+1]-q[i]; if (s[i]<0) s[i]+=100; } s[n-1]=0; build_sa(200); // for (int i=0;i<n;i++) cout<<sa[i]<<" "; for (int i=0;i<n;i++) sa[i]=sa[i+1]; n--; getHeight(); // cout<<endl; // for (int i=1;i<n;i++) cout<<i<<" "<<height[i]<<" "<<sa[i]<<endl; int l=0,r=n; int mid; int zans; while(l<=r){ mid=(l+r)/2; int ans=check(mid); // cout<<l<<" "<<r<<" "<<mid<<" "<<ans<<endl; if (ans) { zans=mid; l=mid+1; } else{ r=mid-1; } } if (zans>=4) zans+=1; else zans=0; printf("%d\n",zans); } return 0; }
标签:ica break math main continue highlight amp uil end
原文地址:http://www.cnblogs.com/xfww/p/7637913.html