B. Hamming Distance Sum
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

 
 

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
Note

For the first sample case, there are four contiguous substrings of b of length |a|: "00", "01", "11", and "11". The distance between "01" and "00" is |0 - 0| + |1 - 0| = 1. The distance between "01" and "01" is |0 - 0| + |1 - 1| = 0. The distance between "01" and "11" is|0 - 1| + |1 - 1| = 1. Last distance counts twice, as there are two occurrences of string "11". The sum of these edit distances is1 + 0 + 1 + 1 = 3.

The second sample case is described in the statement.

题意: 定义两个字符串之间的距离是所有对应的每个字符相减绝对值的和, 给定a和b两个字符串, 求b中与a长度相同的所有子串与a字符串距离之和.

分析: 多写一些样例,就可以归纳出来, 其实就是a字符串中的第一个元素,分别 与b字符串中的第一个元素至第|b|-|a|+1个元素求距离的和, 再加上a字符串中的第2个元素,分别 与b字符串中的第2个元素至第|b|-|a|+2个元素求距离的和......直到a字符串中的第|a|个元素与b字符串中的第|a|个元素至第|b|个元素求距离的总和. 由于求距离很像求异或和, 因此可以先用两个数组分别保存b字符串的0个数的前缀和和1个数的前缀和, 遍历a字符串的时候 ,如果字符是1 ,那么求相应区间0的的个数,  如果字符是0 ,那么求相应区间1的的个数.

AC代码

 #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+,b+))
{
int la=strlen(a+),lb=strlen(b+);
res1[]=res0[]=;
for(int i=; i<=lb; i++)
{
if(b[i]=='')
{
res1[i]=res1[i-]+;
res0[i]=res0[i-];
}
else
{
res1[i]=res1[i-];
res0[i]=res0[i-]+;
}
}
ll ans=;
for(int i=; i<=la; i++)
if(a[i]=='')
ans+=res0[lb-(la-i)]-res0[i-];
else
ans+=res1[lb-(la-i)]-res1[i-];
printf("%I64d\n",ans);
}
return ;
}

2017ecjtu-summer training #2 CodeForces 608B的更多相关文章

  1. HZNU 2019 Summer training 6 -CodeForces - 622

    A - Infinite Sequence  CodeForces - 622A 题目大意:给你一个这样的数列1,1,2,1,2,3,1,2,3,4,1,2,3,4,5....就是从1~n排列(n++ ...

  2. 2017ecjtu-summer training #4 CodeForces 731C

    C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  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. Codeforces Round #588 (Div. 2) D. Marcin and Training Camp(思维)

    链接: https://codeforces.com/contest/1230/problem/D 题意: Marcin is a coach in his university. There are ...

  5. Codeforces Training S03E01泛做

    http://codeforces.com/gym/101078 和ysy.方老师一起打的virtual 打的不是很好...下面按过题顺序放一下过的题的题(dai)解(ma). A 给两个1~n的排列 ...

  6. codeforces 519C. A and B and Team Training 解题报告

    题目链接:http://codeforces.com/contest/519/problem/C 题目意思:给出 n 个  experienced participants  和 m 个 newbie ...

  7. codeforces 519C.. A and B and Team Training

    C. A and B and Team Training time limit per test 1 second memory limit per test 256 megabytes input ...

  8. 【Codeforces 1132D】Stressful Training

    Codeforces 1132 D 题意:给\(n\)个电脑的电量和耗电速度,你可以买一个充电器,它的充电速度是每秒\(v\)单位,\(v\)你自己定.问最小的\(v\)能使得在\(k\)秒内每秒给某 ...

  9. Codeforces 1132D - Stressful Training - [二分+贪心+优先队列]

    题目链接:https://codeforces.com/contest/1132/problem/D 题意: 有 $n$ 个学生,他们的电脑有初始电量 $a[1 \sim n]$,他们的电脑每分钟会耗 ...

随机推荐

  1. 【Uva623】500!(高精)

    Description 求N! \(N \leq 1000\) Sample Input 10 30 50 100 Sample Output 10! 3628800 30! 265252859812 ...

  2. [置顶] MVC中使用signalR入门教程

    一.前言:每次写总要说一点最近的感想 进入工作快半年了,昨天是最郁闷的一天,我怀疑我是不是得了"星期一综合征",每个星期一很没有状态.全身都有点酸痛,这个可能一个星期只有周末才打一 ...

  3. centOS7 jdk安装

    1.查找需要卸载的OpenJDK: #  rpm -qa | grep java 2:依次卸载 rpm -e --nodeps javapackages-tools-3.4.1-6.el7_0.noa ...

  4. bat常用命令

    1.@它的作用是隐藏它后面这一行的命令本身(只能影响当前行).2.echo中文为"反馈"."回显"的意思.它其实是一个开关命令,就是说它只有两种状态:打开和关闭 ...

  5. pwd 命令详解

    pwd 作用: 以绝对路径的方式显示用户当前工作目录,命令将当前目录的全路径名称(从根目录)写入标准输出, 全部目录使用/分隔,第一个/表示根目录, 最后一个/ 表示当前目录. 执行pwd 命令可以立 ...

  6. [经验分享]WebApi+SwaggerUI 完美展示接口

    不喜欢说废话,直接上干货. 第一步: 打开VS IDE ,新建一个WebAPI项目 选择Web .Net FrameWork (如果喜欢使用.Net Core的可以使用.Net Core) 选择Web ...

  7. 尤克里里 ukulele 单板 非kaka tom uma

    本店冲人气优惠,不搞倒闭之类的事 23寸尤克里里 单板 单板 单板 彩贝镶边演出大气 单板 单板 单板 彩贝镶边演出大气 单板 单板 单板 彩贝镶边演出大气 配件选购40元全套(加棉琴包.金属变调夹. ...

  8. Q:同时安装了python2和python3的电脑下pip的使用

    本人电脑设置的默认为python3.5的,由于需要,安装了pyhton2.7.之后需要为python2.7使用pip方式安装一个requests模块,但是,在命令行下执行pip install req ...

  9. 【精选】Nginx模块Lua-Nginx-Module学习笔记(二)Lua指令详解(Directives)

    源码地址:https://github.com/Tinywan/Lua-Nginx-Redis Nginx与Lua编写脚本的基本构建块是指令. 指令用于指定何时运行用户Lua代码以及如何使用结果. 下 ...

  10. Presto集群部署

    前言: 随着大数据的普及,大部分企业的大数据查询与统计渐渐出现瓶颈.虽说存储方面有分布式的HDFS,HBSE,MongoDB等可以应对,但是面对千万级别(1x10^7)界别的数据量查询时,以上组件也不 ...