前缀和思想

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

https://blog.csdn.net/chen_yuazzy/article/details/77074410

根本思想请看https://www.cnblogs.com/mrclr/p/8423136.html

#include<cstdio>
#include<cstring>
#define m 300000
using namespace std;
char a[m];
char b[m];
int pre0[m];
int pre1[m];
int main()
{
int len1,len2,i;
long long s=0;
gets(a);
gets(b);
len1=strlen(a);
len2=strlen(b);
for(i=0;i<len2;i++)
{
if(b[i]=='0') {
pre0[i]=pre0[i-1]+1;
pre1[i]=pre1[i-1];
}
else if(b[i]=='1') {
pre1[i]=pre1[i-1]+1;
pre0[i]=pre0[i-1];
}
}
for(i=0;i<len1;i++)
{
if(a[i]=='0')
{
s+=pre1[i+len2-len1]-pre1[i-1];
}
else if(a[i]=='1')
{
s+=pre0[i+len2-len1]-pre0[i-1];
}
}
printf("%lld",s);
return 0;
}

  

关于前缀和,A - Hamming Distance Sum的更多相关文章

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

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

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

  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. 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 608 B. Hamming Distance Sum-前缀和

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

  8. hdu 4712 Hamming Distance 随机

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  9. hdu 4712 Hamming Distance(随机函数暴力)

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

随机推荐

  1. synchronized锁住的是代码还是对象

    不同的对象 public class Sync { public synchronized void test() { System.out.println("test start" ...

  2. ThreadLocalMap里Entry为何声明为WeakReference?

    Java里,每个线程都有自己的ThreadLocalMap,里边存着自己私有的对象.Map的Entry里,key为ThreadLocal对象,value即为私有对象T.在spring MVC中,常用T ...

  3. Java基础系列--ArrayList集合

    原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/8494618.html 一.概述 ArrayList是Java集合体系中最常使用,也是最简单 ...

  4. openssl rsa/pkey

    openssl系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html openssl rsa和openssl pkey分别是RSA密钥的处理工具和 ...

  5. C#部分类与部分方法

    部分类也可以定义部分方法.部分方法在部分类中定义,但没有方法体,在另一个部分类中执行.在这两个部分类中,都要使用partial关键字. public partial class MyClass { p ...

  6. request请求 HTTPBody 格式

    //Json格式       [mtbRequset setValue:@"application/json" forHTTPHeaderField:@"Content- ...

  7. 通过kubernetes构建ela服务

    一.kubernetes 通过yaml 创建pod与service apiVersion: extensions/v1beta1 kind: Deployment metadata: name: el ...

  8. sublime3安装ctags追踪插件

    sublime3经常要用到函数追踪插件,怎做的?下面看安装步骤: 1.安装package control 按快捷键 ctrl+shift+p 2.安装搜索 ctags插件 3.下载ctags可执行程序 ...

  9. react组件中的constructor和super小知识

    react组件中的constructor和super小知识 1.react中用class申明的类一些小知识 如上图:类Child是通过class关键字申明,并且继承于类React. A.Child的类 ...

  10. java集合类学习

    以下基于jdk1.8 一. 集合类关系图 1. 接口关系图 2.集合中的类,(不包含线程安全的) 二.ArrayList 1.类定义 /** * 用“可伸缩数组”来实现List接口.实现了所有List ...