题目链接:点击打开链接
题解:求两个前缀和,然后暴力匹配一边看有几个相等的数字。就是结果。因为是数字必须连续。所以可以前缀和求解。但是暴力匹配会时间超限。所以需要把前缀和的结果用数组记录一下(类似于桶排序)。
详细实现过程见代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e5+5;
const int maxN = 1e6+5;
int book[maxN];
int main(){
int a[maxn],b[maxn];
int qa[maxn],qb[maxn];
int n,m;
cin >> n >> m;
for(int i = 1 ; i <= n ; i ++){
scanf("%d",&a[i]);
qa[i] = qa[i-1]+a[i];
book[qa[i]]++;
}
for(int j = 1 ; j <= m ; j ++){
scanf("%d",&b[j]);
qb[j] = qb[j-1] + b[j];
}
int ans = 0;
for(int i = 1 ; i <= m ; i ++){
if(book[qb[i]]){
ans ++;
book[qb[i]] --;
}
}
cout << ans << endl;
}