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;
}

Codeforces 608B. Hamming Distance Sum 模拟的更多相关文章

  1. Codeforces Round #336 (Div. 2)B. Hamming Distance Sum 前缀和

    B. Hamming Distance Sum 题目连接: http://www.codeforces.com/contest/608/problem/A Description Genos need ...

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

  3. 关于前缀和,A - Hamming Distance Sum

    前缀和思想 Genos needs your help. He was asked to solve the following programming problem by Saitama: The ...

  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 280D k-Maximum Subsequence Sum [模拟费用流,线段树]

    洛谷 Codeforces bzoj1,bzoj2 这可真是一道n倍经验题呢-- 思路 我首先想到了DP,然后矩阵,然后线段树,然后T飞-- 搜了题解之后发现是模拟费用流. 直接维护选k个子段时的最优 ...

  8. BZOJ 3836 Codeforces 280D k-Maximum Subsequence Sum (模拟费用流、线段树)

    题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=3836 (Codeforces) http://codeforces.com ...

  9. Codeforces 608 B. Hamming Distance Sum-前缀和

      B. Hamming Distance Sum   time limit per test 2 seconds memory limit per test 256 megabytes input ...

随机推荐

  1. 听听八年阿里架构师怎样讲述Dubbo和Spring Cloud微服务架构

    转自:https://baijiahao.baidu.com/s?id=1600174787011483381&wfr=spider&for=pc 微服务架构是互联网很热门的话题,是互 ...

  2. mysql 分片

    MySQL Fabric(分片)  是一个用于管理 MySQL 服务器群的可扩展框架.该框架实现了两个特性 — 高可用性 (HA ) 以及使用数据分片的横向扩展.这两个特性既可以单独使用,也可以结合使 ...

  3. HTML|CSS之布局相关总结

    知识内容: 1.浮动相关 2.display属性 3.居中显示 4.盒模型和box-sizing 5.position属性 6.响应式设计 7.flex布局 8.其他 参考:http://zh.lea ...

  4. html_table表格

    ## `table`表格 表格的常用标签 - `table`表格- `thead`表格头- `tbody`表格主体- `tfoot`表格尾- `th`元素定义表头单元格- `tr`定义表格行- `td ...

  5. (3/24)轻松配置 webpack3.x入口、出口配置项

    在上一节中我们只是简单的尝了一下webpack的鲜,对其有了基本的了解,对于上一节当中的打包方式,在实际开发中并不使用,而是通过webpack的配置文件的方式进行设置的,所以该节就在上一节的基础上学一 ...

  6. 比较完整的URL验证

    转自:http://wuchaorang.2008.blog.163.com/blog/static/4889185220135279223253/ function IsURL(str_url){v ...

  7. leetcode944

    public class Solution { public int MinDeletionSize(string[] A) { ; ; j < A[].Length; j++) { ; i & ...

  8. apache http get 和 post 请求

    1.首先要把jar依赖进项目 <dependency> <groupId>org.apache.httpcomponents</groupId> <artif ...

  9. ABAP-关于 LUW

    转载:https://www.cnblogs.com/liaojunbo/archive/2011/07/12/2103554.html 假设MAIN PROGRAM(调用程序)为MAIN,其所在的为 ...

  10. java.util包简介

    java.util包含集合框架.遗留的 collection 类.事件模型.日期和时间设施.国际化和各种实用工具类(字符串标记生成器.随机数生成器和位数组.日期Date类.堆栈Stack类.向量Vec ...