直接二分长度暴力匹配.......
跑的还挺快
(正解是后缀数组的样子)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 void read(int &x){ 6 char c=getchar();x=0; 7 while(c<‘0‘||c>‘9‘)c=getchar(); 8 while(‘0‘<=c&&c<=‘9‘)x=x*10+(c^48),c=getchar(); 9 } 10 #define N 1002 11 int n,a[N][N],b[N]; 12 bool ask2(int st,int x){ 13 bool p; 14 for(int i=2;i<=n;++i){ 15 p=0; 16 for(int j=1;!p&&j<=b[i]-x+1;++j){ 17 int v=a[1][st]-a[i][j];p=1; 18 for(int k=1;p&&k<x;++k) 19 if(a[1][st+k]!=a[i][j+k]+v)p=0; 20 }if(!p) return 0; 21 }return 1; 22 } 23 bool ask1(int x){ 24 if(x==1) return 1; 25 for(int i=1;i<=b[1]-x+1;++i) 26 if(ask2(i,x)) return 1; 27 return 0; 28 } 29 int main(){ 30 read(n); int k=1; 31 for(int i=1;i<=n;++i){ 32 read(b[i]); 33 if(b[k]>b[i]) k=i; 34 for(int j=1;j<=b[i];++j) read(a[i][j]); 35 }if(k!=1) swap(a[1],a[k]),swap(b[1],b[k]); 36 int l=1,r=b[1],mid; 37 while(l+1<r){ 38 mid=l+((r-l)>>1); 39 if(ask1(mid)) l=mid; 40 else r=mid-1; 41 }printf("%d",ask1(r)?r:l); 42 return 0; 43 }
丧心病狂压行版
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define re return 4 int n,a[1002][1002],b[1002],k; 5 bool ask2(int s,int x){ 6 for(int i=2,j,k;i<=n;++i){bool p=0; 7 for(j=1;!p&&j<=b[i]-x+1;++j){p=1; 8 for(k=1;p&&k<x;++k) 9 if(a[1][s+k]!=a[i][j+k]+a[1][s]-a[i][j])p=0; 10 }if(!p) re 0;}re 1;} 11 bool ask1(int x){for(int i=1;i<=b[1]-x+1;++i)if(ask2(i,x))re 1;re 0;} 12 int main(){cin>>n;k=1; 13 for(int i=1,j;i<=n;++i){cin>>b[i];if(b[k]>b[i])k=i; 14 for(j=1;j<=b[i];++j)cin>>a[i][j]; 15 }if(k!=1)swap(a[1],a[k]),swap(b[1],b[k]); 16 int l=1,r=b[1],mid; 17 while(l+1<r)mid=l+r>>1,ask1(mid)?l=mid:r=mid-1; 18 cout<<(ask1(r)?r:l);}
原文地址:https://www.cnblogs.com/kafuuchino/p/10228462.html
时间: 2024-11-08 04:59:58