其他分享
首页 > 其他分享> > (hash) Number Sequence

(hash) Number Sequence

作者:互联网

Number Sequence

在这里插入图片描述

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

using namespace std;
typedef long long LL;
LL n,m;
LL a[1000010],sumb;
LL p[1000010];
int main(){
    int tx;
    cin>>tx;
    while(tx--){
        cin>>n>>m;
        int t;
        memset(a,0,sizeof a);
        sumb=0;
        p[0]=1;
        for(int i=0;i<n;i++){
            scanf("%d",&t);
            t+=1000001;
            a[i+1]=a[i]*131+t;
            p[i+1]=p[i]*131;
        }
        for(int i=0;i<m;i++){
            scanf("%d",&t);
            t+=1000001;
            sumb=sumb*131+t;
        }

        int flag=-1;
        for(int i=m;i<=n;i++){
            if(sumb==(a[i]-a[i-m]*p[m])){
                flag=i-m+1;
                break;
            }

        }
        if(flag==-1)
            cout<<"-1"<<endl;
        else
            cout<<flag<<endl;
    }
    return 0;
}


标签:hash,tx,Sequence,int,LL,Number,131,include,sumb
来源: https://blog.csdn.net/weixin_46028214/article/details/116381722