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
  1. 01
    00111
Output
  1. 3
Input
  1. 0011
    0110
Output
  1. 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 is 1 + 0 + 1 + 1 = 3.

The second sample case is described in the statement.

题意就是计算距离,拿数据来说,01和00111,就是01和00,01和01,01和11,01和11,就是下面的依次划取和第一组等长的串来计算,划一次就往后走一个数,反正差不多这个意思,而且!!!第二组的长度一定是>=第一组的长度

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int N=2*1e5+10;
  5. ll sum[N][2];
  6. char s1[N],s2[N];
  7. int a[N],b[N];
  8.  
  9. int main(){
  10. scanf("%s%s",s1+1,s2+1);
  11. int len1=strlen(s1+1);
  12. int len2=strlen(s2+1);
  13. for(int i=1;i<=len1;i++)
  14. a[i]=s1[i]-'0';
  15. for(int i=1;i<=len2;i++)
  16. b[i]=s2[i]-'0';
  17. for(int i=1;i<=len2;i++){
  18. for(int j=0;j<2;j++)
  19. sum[i][j]+=sum[i-1][j];
  20. sum[i][b[i]]++;
  21. }
  22. ll ans=0;
  23. for(int i=1;i<=len1;i++){
  24. ans+=sum[len2-len1+i][1-a[i]];
  25. ans-=sum[i-1][1-a[i]];
  26. }
  27. printf("%I64d\n",ans);
  28. return 0;
  29. }

Codeforces 608 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 Round #336 Hamming Distance Sum

    题目: http://codeforces.com/contest/608/problem/B 字符串a和字符串b进行比较,以题目中的第一个样例为例,我刚开始的想法是拿01与00.01.11.11从左 ...

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

  4. Codeforces 608B. Hamming Distance Sum 模拟

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

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

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

  6. Codefroces B. Hamming Distance Sum

    Genos needs your help. He was asked to solve the following programming problem by Saitama: The lengt ...

  7. codeforces 336 Div.2 B. Hamming Distance Sum

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

  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. 对于response.setContentType(MIME)的解释

    response.setContentType(MIME)的作用是使客户端浏览器,区分不同种类的数据,并根据不同的MIME调用浏览器内不同的程序嵌入模块来处理相应的数据.例如web浏览器就是通过MIM ...

  2. Powershell使用真实的对象工作

    Powershell使用真实的对象工作 来源 https://www.pstips.net/powershell-work-with-reallife-objects.html 每一个Powershe ...

  3. 【题解】HAOI2007分割矩阵

    水题盛宴啦啦啦……做起来真的极其舒服,比某些毒瘤题好太多了…… 数据范围极小 --> 状压 / 搜索 / 高维度dp:观察要求的均方差,开始考虑是不是能够换一下式子.我们用\(a_{x}\)来表 ...

  4. [Leetcode] Merge k sorted lists 合并k个已排序的链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思 ...

  5. 【BZOJ 4514】[Sdoi2016]数字配对 费用流

    利用spfa流的性质,我直接拆两半,正解分奇偶(妙),而且判断是否整除且质数我用的是暴力根号,整洁判断质数个数差一(其他非spfa流怎么做?) #include <cstdio> #inc ...

  6. POJ 3179 Corral the Cows

    Corral the Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1352   Accepted: 565 De ...

  7. jsonArray与jsonObject

    最近两个星期接触最多的就是json和map了. 之前用到的json,就是一个键对应一个值,超级简单的一对一关系.现在用到的json那可以层层嵌套啊,刚开始接触的时候,确实有种崩溃的赶脚,不想去理,取个 ...

  8. MyBatis+Spring实现基本CRUD操作

    一.MyBaits介绍   MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架.MyBatis 摒除了大部分的JDBC代码.手工设置参数和结果集重获.MyBatis 只使用简单的X ...

  9. 基于MapReduce的手机流量统计分析

    1,代码 package mr; import java.io.IOException; import org.apache.commons.lang.StringUtils; import org. ...

  10. Exceptioninthread"main"java.lang.ClassNotFoundsException的问题

    报错如下: Exceptioninthread"main"java.lang.ClassNotFoundsException 大致可以判断出是无法定位到main方法,应该是用mav ...