Codeforces 608 B. Hamming Distance Sum-前缀和
2 seconds
256 megabytes
standard input
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|.
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.
Print a single integer — the sum of Hamming distances between a and all contiguous substrings of b of length |a|.
- 01
00111
- 3
- 0011
0110
- 2
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,就是下面的依次划取和第一组等长的串来计算,划一次就往后走一个数,反正差不多这个意思,而且!!!第二组的长度一定是>=第一组的长度
代码:
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const int N=2*1e5+10;
- ll sum[N][2];
- char s1[N],s2[N];
- int a[N],b[N];
- int main(){
- scanf("%s%s",s1+1,s2+1);
- int len1=strlen(s1+1);
- int len2=strlen(s2+1);
- for(int i=1;i<=len1;i++)
- a[i]=s1[i]-'0';
- for(int i=1;i<=len2;i++)
- b[i]=s2[i]-'0';
- for(int i=1;i<=len2;i++){
- for(int j=0;j<2;j++)
- sum[i][j]+=sum[i-1][j];
- sum[i][b[i]]++;
- }
- ll ans=0;
- for(int i=1;i<=len1;i++){
- ans+=sum[len2-len1+i][1-a[i]];
- ans-=sum[i-1][1-a[i]];
- }
- printf("%I64d\n",ans);
- return 0;
- }
Codeforces 608 B. Hamming Distance Sum-前缀和的更多相关文章
- Codeforces Round #336 (Div. 2)B. Hamming Distance Sum 前缀和
B. Hamming Distance Sum 题目连接: http://www.codeforces.com/contest/608/problem/A Description Genos need ...
- Codeforces Round #336 Hamming Distance Sum
题目: http://codeforces.com/contest/608/problem/B 字符串a和字符串b进行比较,以题目中的第一个样例为例,我刚开始的想法是拿01与00.01.11.11从左 ...
- 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 ...
- Codeforces 608B. Hamming Distance Sum 模拟
B. Hamming Distance Sum time limit per test: 2 seconds memory limit per test:256 megabytes input: st ...
- 关于前缀和,A - Hamming Distance Sum
前缀和思想 Genos needs your help. He was asked to solve the following programming problem by Saitama: The ...
- Codefroces B. Hamming Distance Sum
Genos needs your help. He was asked to solve the following programming problem by Saitama: The lengt ...
- codeforces 336 Div.2 B. Hamming Distance Sum
题目链接:http://codeforces.com/problemset/problem/608/B 题目意思:给出两个字符串 a 和 b,然后在b中找出跟 a 一样长度的连续子串,每一位进行求相减 ...
- hdu 4712 Hamming Distance 随机
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- hdu 4712 Hamming Distance(随机函数暴力)
http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...
随机推荐
- 对于response.setContentType(MIME)的解释
response.setContentType(MIME)的作用是使客户端浏览器,区分不同种类的数据,并根据不同的MIME调用浏览器内不同的程序嵌入模块来处理相应的数据.例如web浏览器就是通过MIM ...
- Powershell使用真实的对象工作
Powershell使用真实的对象工作 来源 https://www.pstips.net/powershell-work-with-reallife-objects.html 每一个Powershe ...
- 【题解】HAOI2007分割矩阵
水题盛宴啦啦啦……做起来真的极其舒服,比某些毒瘤题好太多了…… 数据范围极小 --> 状压 / 搜索 / 高维度dp:观察要求的均方差,开始考虑是不是能够换一下式子.我们用\(a_{x}\)来表 ...
- [Leetcode] Merge k sorted lists 合并k个已排序的链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思 ...
- 【BZOJ 4514】[Sdoi2016]数字配对 费用流
利用spfa流的性质,我直接拆两半,正解分奇偶(妙),而且判断是否整除且质数我用的是暴力根号,整洁判断质数个数差一(其他非spfa流怎么做?) #include <cstdio> #inc ...
- POJ 3179 Corral the Cows
Corral the Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1352 Accepted: 565 De ...
- jsonArray与jsonObject
最近两个星期接触最多的就是json和map了. 之前用到的json,就是一个键对应一个值,超级简单的一对一关系.现在用到的json那可以层层嵌套啊,刚开始接触的时候,确实有种崩溃的赶脚,不想去理,取个 ...
- MyBatis+Spring实现基本CRUD操作
一.MyBaits介绍 MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架.MyBatis 摒除了大部分的JDBC代码.手工设置参数和结果集重获.MyBatis 只使用简单的X ...
- 基于MapReduce的手机流量统计分析
1,代码 package mr; import java.io.IOException; import org.apache.commons.lang.StringUtils; import org. ...
- Exceptioninthread"main"java.lang.ClassNotFoundsException的问题
报错如下: Exceptioninthread"main"java.lang.ClassNotFoundsException 大致可以判断出是无法定位到main方法,应该是用mav ...