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. 面试题汇总--数据储存/应用程序/UI控件/客户端的安全性与框架处理。。。

    一 数据储存  1.如果后期需要增加数据库中的字段怎么实现,如果不使用 CoreData 呢?编写 SQL 语句来操作原来表中的字段1)增加表字段ALTER TABLE 表名 ADD COLUMN 字 ...

  2. Elasticsearch索引自动删除

    简介 脚本分2部分,1部分查找符合条件的索引名,2脚本调用1脚本,进行删除操作 脚本 查找符合条件的,默认大于30天 # coding:utf-8 __author__ = 'Jipu FANG' f ...

  3. 如何使用vuex

    一.何为vuex? vuex其实是一种状态管理模式,那么什么是状态管理模式呢?简单的理解就是把一些状态或者数据集中到一个地方管理,然后所有的组件共享这些数据,当数据改变时,用到这些数据的组件也会相应的 ...

  4. github emoji 表情列表

    最新emoji大全:emoji列表 emoji-list emoji表情列表 目录 人物 自然 事物 地点 符号 人物 :bowtie: :bowtie:

  5. 解决CUICatalog: Invalid asset name supplied问题

    这个问题其实是老问题,产生原因就是因为在使用的时候 [UIImage imageNamed:]时,图片不存在或者传入的图片名为nil.

  6. 房上的猫:HTML5基础

    一.W3C标准 1)W3C标准不是某一个标准,而是一系列的标准的集合,一个网页主要由三部分组成,即结构(Structure),表现(Presentation)和行为(Behavior) 2)不很严谨的 ...

  7. Nginx编译配置介绍

    源码包 nginx-1.6.2.tar.gz --help 使用帮助 --prefix=PATH Nginx安装路径,如果没有指定,默认为/usr/local/nginx. --sbin-path=P ...

  8. python导入模块时的执行顺序

    当python导入模块,执行import语句时,到底进行了什么操作?按照python的文档,她执行了如下的操作: 第一步,创建一个新的module对象(它可能包含多个module) 第二步,把这个mo ...

  9. Heroku登录失败

    Heoku 在国内,注册和登录是个大问题,不知道原来怎么注册上了,如今需要登录删除 app 就是删除不了.. 今天努力找了个vpn ,无奈还是登录不成功.https://id.heroku.com/l ...

  10. 微信小程序<web-view>嵌入网页后,小程序如何和网页交互传值?

    最近开发一个项目由于小程序某些组件的限制,然后想到嵌入网页,但是遇到一个问题:网页端调取数据的时候需要 小程序传递多个参数值才能用,如何传值呢? 最初我想到是<web-view src=&quo ...