CodeForces 608 B. Hamming Distance Sum(水~)

Description
定义两个串的距离为这里写图片描述,先给出两个串a和b,求a与b所有长度为|a|的连续子串的距离和
Input
两个01串a和b(1<=|a|<=|b|<=200000)
Output
输出a串与b串所有长度为|a|的连续子串的距离和
Sample Input
01
00111
Sample Output
3
Solution
简单题,累计a串每个字符对答案的贡献,例如对a串第一个字符,其与b串的第1个字符到第|b|-|a|-1个字符会匹配,那么只需要统计b串任意前缀中0和1的数目,如果a串某字符为0,那么就在b串找对应区间中1的个数即为a串这个字符对答案的贡献,是1同理找对应区间中0的个数
Code

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define maxn 222222
typedef long long ll;
char a[maxn],b[maxn];
int res1[maxn],res0[maxn];
int main()
{
    while(~scanf("%s%s",a+1,b+1))
    {
        int la=strlen(a+1),lb=strlen(b+1);
        res1[0]=res0[0]=0;
        for(int i=1;i<=lb;i++)
        {
            res1[i]=res1[i-1]+(b[i]=='1');
            res0[i]=res0[i-1]+(b[i]=='0');
        }
        ll ans=0;
        for(int i=1;i<=la;i++)
            if(a[i]=='1')ans+=res0[lb-(la-i)]-res0[i-1];
            else ans+=res1[lb-(la-i)]-res1[i-1];
        printf("%I64d\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值