1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| #include<bits/stdc++.h> using namespace std; #define ll long long #define pi acos(-1) const int N = 1e6+10; int r[N]; int a[N],b[N],c[N],n,m,nex[N]; const int inf = 0x7f7f7f7f; struct cp { double r,i; cp(double _r = 0,double _i = 0) { r = _r; i = _i; } cp operator +(const cp &b) { return cp(r+b.r,i+b.i); } cp operator -(const cp &b) { return cp(r-b.r,i-b.i); } cp operator *(const cp &b) { return cp(r*b.r-i*b.i,r*b.i+i*b.r); } }F[N],G[N]; void FFT(cp *s,int n,int inv){ int bit=0; while ((1<<bit)<n)bit++; for(int i=0;i<n;i++){ if(i<r[i])swap(s[i],s[r[i]]); } for(int len=2;len<=n;len*=2){ cp wn = cp(cos(pi*2.0/len),inv*sin(pi*2.0/len)); for(int st=0;st<n;st+=len){ cp w = cp(1.0,0.0); for(int i=st;i<st+len/2;i++,w=w*wn){ cp x=s[i]; cp y=w*s[i+len/2]; s[i]=x+y; s[i+len/2]=x-y; } } } } void getnex(){ memset(nex,0,sizeof(nex)); nex[0]=-1; int i=0,k=-1; while(i<m){ if(k==-1||b[i]==b[k])nex[++i]=++k; else k=nex[k]; } } void KMP(){ int i=0,j=0,ans=inf,pos; while(i<n&&j<m){ if(j==-1||a[i]==b[j])i++,j++; else j=nex[j]; if(j==m){ if(ans>c[i-1]){ ans=c[i-1]; pos=i-j+1; } j=nex[j]; } } if(ans==inf)printf("No\n"); else { printf("Yes\n"); printf("%d %d\n",ans,pos); } } void cal(int x,int y,int len){ for(int i=0;i<len;i++){ F[i].i=F[i].r=0; G[i].i=G[i].r=0; } for(int i=0;i<len;i++){ if(i<n&&(a[i]&1)==x)F[i].r=1; if(i<m&&(b[m-i-1]&1)==y)G[i].r=1; } FFT(F,len,1); FFT(G,len,1); for(int i=0;i<len;i++)F[i]=F[i]*G[i]; FFT(F,len,-1); for(int i=0;i<len;i++)c[i]+=(int)(F[i].r/len+0.5); } int main(){ scanf("%d %d",&n,&m); for(int i=0;i<n;i++)scanf("%d",&a[i]); for(int i=0;i<m;i++)scanf("%d",&b[i]); int bit=0,len=1; while(len<n+m)len<<=1,bit++; for(int i=0;i<len;i++)r[i]=(r[i>>1]>>1)|((i&1)<<(bit-1)); cal(0,1,len); cal(1,0,len); for(int i=0;i<n;i++)a[i]/=10; for(int i=0;i<m;i++)b[i]/=10; getnex(); KMP(); }
|