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|.

Sample test(s)
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.

题意:输入两个只包含1和0的字符串a,b(1≤|a| ≤ |b| ≤ 200 000)。a与b中所有相等长度的连续子串进行比较,结果为每位数差的绝对值之和。输出所有结果的和。

样例1:a是01,b是00111;01与00(b中的第1位和第2位)比较,01与01(b中的第2位和第3位)比较,01与11(b中的第3位和第4位)比较,01与11(b中的第4位和第5位)比较。每一个比较的结果分别为1,0,1,1。输出结果的和3。

思路分析:a中的第1个数要和b中的第1,2,3,4个数进行比较,a中的第2个数要和b中的第2,3,4,5个数进行比较。a中的每一个数都比较了4次,设字符串a的长度为lena,b的长度为lenb,那就是要比较compare=lenb-lena+1次。每一次比较只要统计当前区间 [ b[i],b[i+compare])b为1的个数sum,如果a[i]=1,就加上compare-sum;如果a[i]=0,就加上sum。第一次sum的值就是for(i=0;i<compare;i++) if(b[i]==1) sum++;以后每次比较完之后就只要if(b[i]==1) sum--;if(b[i+compare]==1) sum++;

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
string a,b;
int na,nb,compare;
cin>>a>>b;
na=a.size();
nb=b.size();
compare=nb-na+;
int i;
__int64 sum=,ans=;
for(i=; i<compare; i++)
if(b[i]=='')sum++;
for(i=; i<na; i++)
{
if(a[i]=='') ans+=compare-sum;
else ans+=sum;
if(b[i]=='')sum--;
if(b[i+compare]=='')sum++;
}
cout<<ans<<endl;
}

最新文章

  1. sh 自动化安装配置FTP服务器
  2. 两种方法实现用CSS切割图片只取图片中一部分
  3. python __call__内置函数
  4. ECMAScript 6中的let和const关键词
  5. CSS文本方向
  6. HDU1257
  7. 树莓派3b+ 用samba与windows共享文件
  8. 【Todo】用python进行机器学习数据模拟及逻辑回归实验
  9. hdu 4352 XHXJ&#39;s LIS 数位DP
  10. 简单OS(ucos超级精简版)&mdash;&mdash;裸调度器【worldsing笔记】
  11. dispatchkeyevent的调用机制
  12. android 简单粗暴的注解初始化View学习
  13. linux最小安装
  14. java zip解压
  15. 【mongodb系统学习之七】mongodb的关闭
  16. Jest 学习笔记(一)之matchers
  17. 关于mdb数据库在插入过程中报错-&gt;Syntax error in INSERT INTO statement.(sql语句没问题)
  18. JQuery基础-- Ajax
  19. print(array)时array中间是省略号没有输出全部的解决方法
  20. android的download manager(1)

热门文章

  1. 深入分析Java Web技术内幕 修订版 pdf
  2. 图解http pdf
  3. float属性详解
  4. Python之函数——基础篇
  5. python pip使用报错:Fatal error in launcher: Unable to create process using &#39;&quot;&#39;
  6. corejava-内容梳理
  7. python3中的mysql数据库操作
  8. JS - 循环,条件if,switch,while
  9. UVA-712-满二叉树
  10. PHP Token(令牌)设计 避免重复提交