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 ...
随机推荐
- Sciter使用心得
1. div双击事件 $(div).onMouse = function(evt) { switch(evt.type) { case Event.MOUSE_DCLI ...
- 【python】python+behave自动化
留坑,后面再写,先写下request对http请求的校验.
- Selenium2Library系列 keywords 之 _SelectElementKeywords 之 select_from_list_by_value(self, locator, *values)
def select_from_list_by_value(self, locator, *values): """Selects `*values` from list ...
- FreeMarker笔记 前言&第1章 入门
简介 简介 FreeMarker是一款模板引擎:一种基于模板的.用来生成输出文本(任何来自于HTML格式的文本用来自动生成源代码)的通用工具.它是为Java程序员提供的一个开发包或者说是类库.它不是面 ...
- linux 学习网站
study-Area:http://www.study-area.org 鸟哥的私房菜:http://linux.vbird.org 鸟哥的私房菜课后答案:http://wapwenku.baidu. ...
- Lucene 入门需要了解的东西
全文搜索引擎的原理网上大段的内容,要想深入的学习,最好的办法就是先用一下,lucene 发展比较快,下面是写第一个demo 要注意的一些事情: 1.Lucene的核心jar包,下面几个包分别位于不同 ...
- hadoop 异常及处理总结-01(小马哥-原创)
试验环境: 本地:MyEclipse 集群:Vmware 11+ 6台 Centos 6.5 Hadoop版本: 2.4.0(配置为自动HA) 试验背景: 在正常测试MapReduce(下简称MR)程 ...
- HW7.17
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- 使用ReflectionTestUtils解决依赖注入
概述 当使用junit来测试Spring的代码时,为了减少依赖,需要给对象的依赖,设置一个mock对象,但是由于Spring可以使用@Autoware类似的注解方式,对私有的成员进行赋值,此时无法 ...
- Xcode集成Google Test
Xcode集成Google Test 1.下载源代码https://github.com/google/googletest 2.进入下载文件目录下googletest/make中运行make gte ...