前缀和思想

Genos needs your help. He was asked to solve the following programming problem by Saitama:

The length of some string s is denoted |s|. The Hamming distance between two strings s and t of equal length is defined as , where si is the i-th character of s and ti is the i-th character of t. For example, the Hamming distance between string "0011" and string "0110" is |0 - 0| + |0 - 1| + |1 - 1| + |1 - 0| = 0 + 1 + 0 + 1 = 2.

Given two binary strings a and b, find the sum of the Hamming distances between a and all contiguous substrings of b of length |a|.

Input

The first line of the input contains binary string a (1 ≤ |a| ≤ 200 000).

The second line of the input contains binary string b (|a| ≤ |b| ≤ 200 000).

Both strings are guaranteed to consist of characters '0' and '1' only.

Output

Print a single integer — the sum of Hamming distances between a and all contiguous substrings of b of length |a|.

Examples

Input
01
00111
Output
3
Input
0011
0110
Output
2

https://blog.csdn.net/chen_yuazzy/article/details/77074410

根本思想请看https://www.cnblogs.com/mrclr/p/8423136.html

#include<cstdio>
#include<cstring>
#define m 300000
using namespace std;
char a[m];
char b[m];
int pre0[m];
int pre1[m];
int main()
{
int len1,len2,i;
long long s=0;
gets(a);
gets(b);
len1=strlen(a);
len2=strlen(b);
for(i=0;i<len2;i++)
{
if(b[i]=='0') {
pre0[i]=pre0[i-1]+1;
pre1[i]=pre1[i-1];
}
else if(b[i]=='1') {
pre1[i]=pre1[i-1]+1;
pre0[i]=pre0[i-1];
}
}
for(i=0;i<len1;i++)
{
if(a[i]=='0')
{
s+=pre1[i+len2-len1]-pre1[i-1];
}
else if(a[i]=='1')
{
s+=pre0[i+len2-len1]-pre0[i-1];
}
}
printf("%lld",s);
return 0;
}

  

关于前缀和,A - Hamming Distance Sum的更多相关文章

  1. Codeforces Round #336 (Div. 2) B. Hamming Distance Sum 计算答案贡献+前缀和

    B. Hamming Distance Sum   Genos needs your help. He was asked to solve the following programming pro ...

  2. Codeforces Round #336 (Div. 2)B. Hamming Distance Sum 前缀和

    B. Hamming Distance Sum 题目连接: http://www.codeforces.com/contest/608/problem/A Description Genos need ...

  3. Codeforces 608B. Hamming Distance Sum 模拟

    B. Hamming Distance Sum time limit per test: 2 seconds memory limit per test:256 megabytes input: st ...

  4. Codefroces B. Hamming Distance Sum

    Genos needs your help. He was asked to solve the following programming problem by Saitama: The lengt ...

  5. Codeforces Round #336 Hamming Distance Sum

    题目: http://codeforces.com/contest/608/problem/B 字符串a和字符串b进行比较,以题目中的第一个样例为例,我刚开始的想法是拿01与00.01.11.11从左 ...

  6. codeforces 336 Div.2 B. Hamming Distance Sum

    题目链接:http://codeforces.com/problemset/problem/608/B 题目意思:给出两个字符串 a 和 b,然后在b中找出跟 a 一样长度的连续子串,每一位进行求相减 ...

  7. Codeforces 608 B. Hamming Distance Sum-前缀和

      B. Hamming Distance Sum   time limit per test 2 seconds memory limit per test 256 megabytes input ...

  8. hdu 4712 Hamming Distance 随机

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  9. hdu 4712 Hamming Distance(随机函数暴力)

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

随机推荐

  1. javascript的作用域和优先级

    变量的作用域是在定义时决定的,不是在运行时活动对象是在运行时决定的?如果就创建一个对象,使用完毕就完了,就使用json字面量的方式如果对象被反复创建,反复使用,就使用自定义的构造函数方式优先级内部变量 ...

  2. 【学习笔记】AJAX内容拓展

    题记——近期回顾<javaScript高级程序设计>ajax章节,发现书中对封装一个ajax库并不完美,结合之前看到博客,对ajax库做一点点的优化. 参考博客:如何去封装一个ajax库 ...

  3. nginx作为web服务以及nginx.conf详解

    Nginx系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html 1.nginx简介 nginx是一个优秀的web服务程序.反向代理程序.它采用非 ...

  4. 微服务学习二:springboot与swagger2的集成

    现在测试都提倡自动化测试,那我们作为后台的开发人员,也得进步下啊,以前用postman来测试后台接口,那个麻烦啊,一个字母输错就导致测试失败,现在swagger的出现可谓是拯救了这些开发人员,便捷之处 ...

  5. ASP.NET Core Identity 实战(2)——注册、登录、Claim

    上一篇文章(ASP.NET Core Identity Hands On(1)--Identity 初次体验)中,我们初识了Identity,并且详细分析了AspNetUsers用户存储表,这篇我们将 ...

  6. VB.NET语法小结

    本人精通C#编程,VB没有开发经验,项目维护需要,特意整理了下VB语法,进行恶补.编程思想都是互通的,都是微软生的,语言大同小异. Imports System 一.(1)定义一个变量,并且初始化. ...

  7. ELK日志分析平台系统CentOS7环境搭建和基本使用

    一.搭建环境 系统环境:CentOS7 安装iptables:https://blog.csdn.net/momo_mutou/article/details/81739155 jdk1.8:  ht ...

  8. PHP7.27: connect mysql 5.7 using new mysqli

    <!doctype html> <html> <head> <meta name="viewport" content="wid ...

  9. HTML5本地存储localStorage与sessionStorage详解

    前言 在最近的项目中用到了html5的本地存储,下面总结一下. 1.html5几种存储形式 本地存储(localStorage && sessionStorage) 离线缓存(appl ...

  10. Nginx 负载均衡原理简介与负载均衡配置详解

    Nginx负载均衡原理简介与负载均衡配置详解   by:授客  QQ:1033553122   测试环境 nginx-1.10.0 负载均衡原理 客户端向反向代理发送请求,接着反向代理根据某种负载机制 ...