B. Hamming Distance Sum
 

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.

 题意:给你两个串 a,b;

对于  "0011" , "0110"   价值就是  |0 - 0| + |0 - 1| + |1 - 1| + |1 - 0| = 0 + 1 + 0 + 1 = 2.

a的长度严格小于等于b,a从b其实对应位置开始从右移到a,b末尾位置对应,问你在这一个过程中 价值是多少

题解:我们就  计算对于b串每一个元素  所取得的价值是多少就好了,算个前缀就好

//meek///#include<bits/stdc++.h>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<iostream>
#include<bitset>
using namespace std ;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
typedef long long ll; const int N = ;
const int inf = 0x3f3f3f3f;
const int MOD = ;
const double eps = 0.000001; char a[N],b[N];
int sum[N],hou[N];
int main()
{
scanf("%s%s",a,b);
int lena=strlen(a);
for(int i=;i<lena;i++) {
sum[i+] = sum[i]+a[i]-'';
}
ll ans=;
int len=strlen(b);
for(int i=;i<len;i++) {
b[i]-='';
}
for(int i=;i<len;i++) {
int l,r;
if(i+>=lena) r=lena;
else r=i+;
if(i+>=lena) {
if(i+lena<=len) l=;
else l=(i+)-(len-lena);
}
else {
if(len-lena>=i+) l=;
else {
l=i+-(len-lena);
}
} if(b[i]==) {
ans += (r-l+)-(sum[r]-sum[l-]);
}
else ans+= (sum[r]-sum[l-]);
}
cout<<ans<<endl;
return ;
}

代码

Codeforces Round #336 (Div. 2) B. 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 336 Div.2 B. Hamming Distance Sum

    题目链接:http://codeforces.com/problemset/problem/608/B 题目意思:给出两个字符串 a 和 b,然后在b中找出跟 a 一样长度的连续子串,每一位进行求相减 ...

  3. Codeforces Round #336 (Div. 2) D. Zuma

    Codeforces Round #336 (Div. 2) D. Zuma 题意:输入一个字符串:每次消去一个回文串,问最少消去的次数为多少? 思路:一般对于可以从中间操作的,一般看成是从头开始(因 ...

  4. Codeforces Round #336 (Div. 2)【A.思维,暴力,B.字符串,暴搜,前缀和,C.暴力,D,区间dp,E,字符串,数学】

    A. Saitama Destroys Hotel time limit per test:1 second memory limit per test:256 megabytes input:sta ...

  5. Codeforces Round #336 (Div. 2)

    水 A - Saitama Destroys Hotel 简单的模拟,小贪心.其实只要求max (ans, t + f); #include <bits/stdc++.h> using n ...

  6. Codeforces Round #336 (Div. 2)B 暴力 C dp D 区间dp

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

  7. Codeforces Round #336 (Div. 2) C. Chain Reaction set维护dp

    C. Chain Reaction 题目连接: http://www.codeforces.com/contest/608/problem/C Description There are n beac ...

  8. Codeforces Round #336 (Div. 2)C. Chain Reaction DP

    C. Chain Reaction   There are n beacons located at distinct positions on a number line. The i-th bea ...

  9. Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索

    D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...

随机推荐

  1. hdu 5104 Primes Problem

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5104 Primes Problem Description Given a number n, ple ...

  2. Golang与MySQL

    1. 在golib下载go-sql-driver/mysql go get github.com/go-sql-driver/mysql 2. 代码引入 import ( "database ...

  3. Charles移动端抓包工具使用

    软件Charle 是一个HTTP代理服务器,HTTP监视器,反转代理服务器.它允许一个开发者查看所有连接互联网的HTTP通信.这些包括request, response现HTTP headers (包 ...

  4. xcode 产生指定颜色的图片imageWithColor

    是在万能的stackOverflow上找到的答案,留下了, 原地址:http://stackoverflow.com/questions/6496441/creating-a-uiimage-from ...

  5. iOS 进阶 第二十二天(0603)

    0603 block\运行时 block block的本质是一个指向结构体的指针. 运行时 要分析clang命令反编译出来的c++代码,就要把一些小括号删掉来分析.因为这些小括号一般都是类型强转. o ...

  6. iOS 进阶 第十七天(0420)

    0420 凡是继承了UIResponder的类都可以做响应者 响应事件的传递是由底到高来传递,响应者链条是由高到底来响应 相应事件的传递(由底到高 找到正在和用户触摸交互的view) 准则:事件由父控 ...

  7. 在本地环境用虚拟机win2008 sever搭建VS2013 + SVN 代码版本控制环境

    此文仅仅是自己笔记做个备忘.因为自己开发一些中小型的软件经常需要修修改改,特别是winform界面的大改动.经常需要对版本进行管理.而租用分布式服务器和远程服务器都不是自己想要的.本文结合虚拟机 + ...

  8. ED/EP简介

    ED:electronic Deposit,电子存折 EP:electronic Purse,电子钱包 PIN:personal identification number,个人识别码 MAC:Mes ...

  9. Careercup - Microsoft面试题 - 5428361417457664

    2014-05-11 03:37 题目链接 原题: You have three jars filled with candies. One jar is filled with banana can ...

  10. 【Binary Tree Post order Traversal】cpp

    题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...