标签:false structure size const char void open event pen
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 27539 | Accepted: 9290 |
Description
Input
Output
Sample Input
30 25 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 18 82 78 74 70 66 67 64 60 65 80 0
Sample Output
5
Hint
Source
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int N=2e4+5,INF=1e9; inline int read(){ char c=getchar();int x=0,f=1; while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1; c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘; c=getchar();} return x*f; } int n,m,k; int s[N]; int sa[N],c[N],t1[N],t2[N]; inline bool cmp(int *r,int a,int b,int j){ return a+j<=n&&b+j<=n&&r[a]==r[b]&&r[a+j]==r[b+j]; } int rnk[N],height[N]; void getHeight(){ int k=0; for(int i=1;i<=n;i++) rnk[sa[i]]=i; for(int i=1;i<=n;i++){ if(k) k--; if(rnk[i]==1) {continue;} int j=sa[rnk[i]-1]; while(s[i+k]==s[j+k]) k++; height[rnk[i]]=k; } } void buildSA(){ int *r=t1,*k=t2; for(int i=0;i<=m;i++) c[i]=0; for(int i=1;i<=n;i++) c[r[i]=s[i]]++; for(int i=1;i<=m;i++) c[i]+=c[i-1]; for(int i=n;i>=1;i--) sa[c[r[i]]--]=i; for(int j=1;j<=n;j<<=1){//printf("hij %d\n",j); int p=0; for(int i=n-j+1;i<=n;i++) k[++p]=i; for(int i=1;i<=n;i++) if(sa[i]>j) k[++p]=sa[i]-j; for(int i=0;i<=m;i++) c[i]=0; for(int i=1;i<=n;i++) c[r[k[i]]]++; for(int i=1;i<=m;i++) c[i]+=c[i-1]; for(int i=n;i>=1;i--) sa[c[r[k[i]]]--]=k[i]; swap(r,k);p=0;r[sa[1]]=++p; for(int i=2;i<=n;i++) r[sa[i]]=cmp(k,sa[i],sa[i-1],j)?p:++p; if(p>=n) break;m=p; } getHeight(); } bool check(int mid){ int mn=sa[1],mx=sa[1]; for(int i=2;i<=n;i++){ if(height[i]>=mid){ mn=min(mn,sa[i]); mx=max(mx,sa[i]); if(mx-mn>mid) return true; }else mn=mx=sa[i]; } return false; } void solve(){ //for(int i=1;i<=n;i++) printf("%d ",sa[i]);puts(""); //for(int i=1;i<=n;i++) printf("%d ",height[i]);puts(""); int l=1,r=n; while(l<r){ int mid=(l+r+1)>>1; if(check(mid)) l=mid; else r=mid-1; } if(l+1<5) puts("0"); else printf("%d\n",l+1); } int main(){ //freopen("in.txt","r",stdin); while((n=read())){ if(n==1) {printf("%d\n",0);continue;} for(int i=1;i<=n;i++) s[i]=read();n--; for(int i=1;i<=n;i++) s[i]=s[i+1]-s[i]+100;m=200; buildSA(); solve(); } }
标签:false structure size const char void open event pen
原文地址:http://www.cnblogs.com/candy99/p/6227659.html