Codeforces Round #469 (Div. 2) B. Intercepted Message

B. Intercepted Message

Hacker Zhorik wants to decipher two secret messages he intercepted yesterday. Yeah message is a sequence of encrypted blocks, each of them consists of several bytes of information.

Zhorik knows that each of the messages is an archive containing one or more files. Zhorik knows how each of these archives was transferred through the network: if an archive consists of k files of sizes l1, l2, ..., lk bytes, then the i-th file is split to one or more blocks bi, 1, bi, 2, ..., bi, mi (here the total length of the blocks bi, 1 + bi, 2 + ... + bi, mi is equal to the length of the file li), and after that all blocks are transferred through the network, maintaining the order of files in the archive.

Zhorik thinks that the two messages contain the same archive, because their total lengths are equal. However, each file can be split in blocks in different ways in the two messages.

You are given the lengths of blocks in each of the two messages. Help Zhorik to determine what is the maximum number of files could be in the archive, if the Zhorik's assumption is correct.

Input

The first line contains two integers nm (1 ≤ n, m ≤ 105) — the number of blocks in the first and in the second messages.

The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 106) — the length of the blocks that form the first message.

The third line contains m integers y1, y2, ..., ym (1 ≤ yi ≤ 106) — the length of the blocks that form the second message.

It is guaranteed that x1 + ... + xn = y1 + ... + ymAlso, it is guaranteed that x1 + ... + xn ≤ 106.

Output

Print the maximum number of files the intercepted array could consist of.

Examples
input
Copy
7 6
2 5 3 1 11 4 4
7 8 2 4 1 8
output
3
input
Copy
3 3
1 10 100
1 100 10
output
2
input
Copy
1 4
4
1 1 1 1
output
1

题意:给你两个数组a,b,保证a的元素总和等于b总和。让你把两个数组划分成几个部分,要求a第一段等于b第一段,a第二段等于b第二段,以此类推。问你最多划分几个部分。

思路:前缀和扫一遍就可以了。由于这几个部分是连续的,所以只要找两个数组前缀和相等的位置就可以了,有几个位置答案就是几,两个数组进行匹配时,先标记第一个数组,此时第二个扫一遍即可,O(n)复杂度。如果数据较大也可以用map标记。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long
using namespace std;
mapmp;
ll n,m,x,sum;
int main()
{
    while(~scanf("%lld%lld",&n,&m))
    {
        mp.clear();
        sum=x=0;
        for(ll i=1;i<=n;i++)
        {
            scanf("%lld",&x);
            sum+=x;
            mp[sum]=1;
        }
        sum=0;
        ll ans=0;
        for(ll i=1;i<=m;i++)
        {
            scanf("%lld",&x);
            sum+=x;
            if(mp[sum]==1)ans++;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

你可能感兴趣的:(Codeforces)