Codeforces Round #336 (Div. 2)B. Hamming Distance Sum 前缀和
B. Hamming Distance Sum
题目连接:
http://www.codeforces.com/contest/608/problem/A
Description
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 Input
01
00111
Sample Output
3
Hint
题意
给你一个a串,和一个b串,让A串去依次匹配b[0]-b[lena-1],b[1]-b[lena],b[2]-b[lena+].....
然后权值就是上下相减的绝对值
问你最后的权值和是多少
题解:
记录B串的前缀和,对于A串的每个字母而言,他所花费的代价,就是他移动的区间中,和他不一样的数的个数就好了
直接扫一遍就OK
代码
#include<bits/stdc++.h>
using namespace std;
#define maxn 200005
long long sum[maxn][2];
char a[maxn],b[maxn];
int A[maxn],B[maxn];
int main()
{
scanf("%s%s",a+1,b+1);
int len = strlen(a+1),len2 = strlen(b+1);
for(int i=1;i<=len;i++)
A[i]=a[i]-'0';
for(int i=1;i<=len2;i++)
B[i]=b[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]]++;
}
long long ans = 0;
for(int i=1;i<=len;i++)
{
ans+=sum[len2-len+i][1-A[i]];
ans-=sum[i-1][1-A[i]];
}
cout<<ans<<endl;
}
Codeforces Round #336 (Div. 2)B. Hamming Distance Sum 前缀和的更多相关文章
- 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 336 Div.2 B. Hamming Distance Sum
题目链接:http://codeforces.com/problemset/problem/608/B 题目意思:给出两个字符串 a 和 b,然后在b中找出跟 a 一样长度的连续子串,每一位进行求相减 ...
- Codeforces Round #336 (Div. 2)-608A.水题 608B.前缀和
A题和B题... A. Saitama Destroys Hotel time limit per test 1 second memory limit per test 256 megabyte ...
- Codeforces Round #336 (Div. 2) D. Zuma
Codeforces Round #336 (Div. 2) D. Zuma 题意:输入一个字符串:每次消去一个回文串,问最少消去的次数为多少? 思路:一般对于可以从中间操作的,一般看成是从头开始(因 ...
- 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 ...
- Codeforces Round #336 (Div. 2)
水 A - Saitama Destroys Hotel 简单的模拟,小贪心.其实只要求max (ans, t + f); #include <bits/stdc++.h> using n ...
- 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 ...
- Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和
Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- 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 ...
随机推荐
- android edittext不弹出软键盘
方法一: 在AndroidMainfest.xml中选择哪个activity,设置windowSoftInputMode属性为adjustUnspecified|stateHidden 例如:< ...
- n个数的最小公倍数
Description 求n个数的最小公倍数. Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数. Output 为每组测试数据输出它们的最小公倍数,每个测 ...
- 实现3D摄像机缓冲系统的一些思考
最近需要模拟红侠乔伊的镜头运用.这东西初看简单,实际还是很需要功夫的.关键不是程序技术如何(就一个摄像机),而是分析其轨迹和追踪点规律.其实就是一个3D空间中的缓冲系统.你如何确定都有什么参数,这么多 ...
- 【LeetCode】101 - Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 使用U盘安装win7系统,遇到“无法定位现有系统分区”问题
朋友的本子貌似因为安装360wifi而导致一进入系统就蓝屏重启,虽然之后就卸载了360wifi,但是问题依旧,上网Google了一下,发觉网上不少网友诉苦,也有人分析原因,说是因为360wifi导致了 ...
- Java 操作MySql数据库
Java 项目开发中数据库操作是很重要的一个方面,对于初学者来说,MySql是比较容易熟悉的一种常见数据库,这篇文章记录了如何用Java来操作MySql数据库. 第一章 JDBC的概念 JDBC(Ja ...
- 徐汉彬:亿级Web系统搭建——单机到分布式集群(转载)
文章转载自http://www.csdn.net/article/2014-11-06/2822529/1 当一个Web系统从日访问量10万逐步增长到1000万,甚至超过1亿的过程中,Web系统承受的 ...
- Hadoop 集群常见错误
这里将自己在初识hadoop过程中遇到的一些错误做一个简单总结: (一)启动hadoop集群时易出现的错误: 错误现象:java.net.NoRouteToHostException: No rout ...
- java Study 基础 1
1.myEclipse 生成get.set,source>Generate getter and setter 2.Web servlet.HttpServlet.HttpServletRequ ...
- 使用IIS6.0遇到问题后,常用的几种解决方法
1.检查 .Net Framework,是否安装完全,不确定的情况下使用:aspnet_regiis.exe -i 或者 aspnet_regiis.exe -r 2.检查 IIS 6.0 其它相关配 ...