前缀和思想

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. EntityFramework附加实体

    //0.0创建修改的 实体对象 Models.BlogArticle model = new BlogArticle(); model.AId = 12; model.ATitle = "新 ...

  2. Maven教程2(Eclipse配置及maven项目)

    Maven教程1(介绍安装和配置) Eclipse中配置maven 1.Eclipse中默认集成的有Maven 可以使用默认的,本地仓库在当前用户下的.m2文件夹下. 2.配置我们自己安装的maven ...

  3. Spring之IOC容器

    在前面博客中介绍什么是依赖注入时有提到:依赖注入是组件之间依赖关系由容器在运行期决定,即由容器动态的将某个依赖关系注入到组件之中.那什么是容器?既然Spring框架实现了IOC,那Spring中的容器 ...

  4. foreach 引发的值类型与引用类型思考

    用都知道的一句话概括:“引用类型在堆上,栈上只保存引用:值类型即可存放于栈上也可存放于堆上,值类型变量直接存储值本身”. class Program { static void Main(string ...

  5. vb.net 使用NPOI控制Excel檔

    '導入命名空間 Imports NPOI.HSSF.UserModelImports NPOI.HPSFImports NPOI.POIFS.FileSystem Private Sub A1()'方 ...

  6. 13.Linux键盘按键驱动 (详解)

    在上一节分析输入子系统内的intput_handler软件处理部分后,接下来我们开始写input_dev驱动 本节目标: 实现键盘驱动,让开发板的4个按键代表键盘中的L.S.空格键.回车键 1.先来介 ...

  7. Flask 中的 特殊装饰器before_request/after_request

    before_request :在请求收到之前绑定一个函数做一些事情. after_request: 每一个请求之后绑定一个函数,如果请求没有异常. teardown_request: 每一个请求之后 ...

  8. (三)Sass和Compass--制作精灵图片

    6.1 精灵的工作原理 // 将各种图片合并到一张图片里面,并在不同的状态下改变背景图片的位置; 6.2 精灵的重要性 // 压缩图片的内存; // 减少HTTP请求 6.2.3 Compass处理精 ...

  9. 腾讯Tars环境搭建 ---- centos

    1,安装git yum install git 2,下载脚本 git clone https://github.com/tangramor/Tars_Install.git 注意:会有3个脚本,cen ...

  10. Nginx 反向代理工作原理简介与配置详解

    Nginx反向代理工作原理简介与配置详解   by:授客  QQ:1033553122   测试环境 CentOS 6.5-x86_64 nginx-1.10.0 下载地址:http://nginx. ...