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. PDO绑定含IN的SQL语句的参数注意事项

    PDOStatement::bindParam(),表示将PDO::prepare()语句中的占位符用参数替换掉.注意,在绑定含有IN的SQL多参数语句时要额外小心,比如$stmt = $db-> ...

  2. 后缀数组之hihocoder 重复旋律1-4

    蒟蒻知道今天才会打后缀数组,而且还是nlogn^2的...但基本上还是跑得过的: 重复旋律1: 二分答案,把height划分集合,height<mid就重新划分,这样保证了每个集合中的LCP&g ...

  3. 简单记录一下原生ajax

    面试老忘记,代码如下 function ajax() { var xmlHttpRequest = null; //定义XMLHttp对象的容器 if(window.XMLHttpRequest) { ...

  4. 关于table布局的推荐使用原因

    一.关于table布局的性能 1.table标签比其他html标签占用更多字节,导致下载时间延迟,占用服务器更多的流量资源: 2.table会阻碍浏览器渲染引擎的渲染顺序,导致页面生成的延迟,造成不良 ...

  5. Python 接口测试(十)

    这里对接口测试9 进行优化升级,前端进行重构后的代码,源码已经开源 经过将近一个月的编写 , TIAPTest 接口测试平台 , 已经部署到服务器,开始运行了. http://60.205.187.1 ...

  6. C# 字符串的连接

    1.利用 "+"(加号)运算符: string str = "Hello"+ "World": console.WriteLine(str) ...

  7. Java Web高级编程(一)

    Servlet 一.创建Servlet类 在Java EE中,Servlet用来接收和响应终端用户的请求.Servlet是所有Web应用程序的核心类,是唯一既可以直接处理和响应用户请求,也可以将处理工 ...

  8. Jenkins 学习笔记

    Jenkins 的内容网站蛮多的,但是一开始我看起来确实很费劲.似乎好多东西都是悬空的,没有把底层的信息交代清楚. 我把自己对于 Jenkins 的探索过程记录下来,如下. 目录 Jenkins 学习 ...

  9. Netty对Protocol Buffer多协议的支持(八)

    Netty对Protocol Buffer多协议的支持(八) 一.背景 在上篇博文中笔者已经用代码演示了如何在netty中使用Protocol Buffer,然而细心的用户可能会发现一个明显的不足之处 ...

  10. mysql优化-》查询缓存

    使用MySql查询缓存(query_cache_size) 在MySql中查询缓存的原理: 其实是MySql创建了一个临时的空间叫Qcache(这个空间生成在MySql的编译器内存中),这个空间的大小 ...