题解 | #音乐研究#
音乐研究
https://ac.nowcoder.com/acm/problem/13222
思路
暴力枚举,O(nm) 注意要等长,所以母串最多到n-m
代码
#pragma GCC optimize("Ofast", "inline", "-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define int long long
using namespace std;
const int N=2e5+7;
const int mod=1e9+7;
//int read(){ int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') f=f*-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
int n,m,a[N],b[N],ans=inf;
signed main(){
// ios::sync_with_stdio(0);
// cin.tie(0);cout.tie(0);
// freopen("in.cpp","r",stdin);
// freopen("out.cpp","w",stdout);
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
cin>>m;
for(int i=1;i<=m;i++) cin>>b[i];
for(int i=0;i<=m-n;i++){
int tmp=0;
for(int j=1;j<=n;j++){
tmp+=(b[i+j]-a[j])*(b[i+j]-a[j]);
}
ans=min(tmp,ans);
}
cout<<ans<<"\n";
return 0;
}