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 ...
随机推荐
- Getting and installing the PEAR package manager
Windows After you have downloaded and installed PHP, you have to manually execute the batch file loc ...
- javascript防止SQL注入
<SCRIPT language="javascript">function Check(theform){ if (theform.UserName.value== ...
- 什么是CC攻击,如何防止网站被CC攻击的方法总汇
CC攻击(Challenge Collapsar)是DDOS(分布式拒绝服务)的一种,也是一种常见的网站攻击方法,攻击者通过代理服务器或者肉鸡向向受害主机不停地发大量数据包,造成对方服务器资源耗尽,一 ...
- linux 学习网站
study-Area:http://www.study-area.org 鸟哥的私房菜:http://linux.vbird.org 鸟哥的私房菜课后答案:http://wapwenku.baidu. ...
- 一个有趣的模拟光照的shader(类似法线贴图)
最近使用unity,碰到到一个很有趣的例子.场景无光线,却模拟出了光照,效果挺好.其思路与法线贴图原理异曲同工. 原作者提供的效果印象深刻. 模型除了使用原来的diffuse贴图外,还用到了一张模拟记 ...
- VS2013 调试MVC源码[MVC5.2.3+MVC4Web项目]
1.目前MVC源码版本为5.2.3,下回来后用VS2013打开,把System.Web.Mvc项目的版本号改为4.0.0.1 2.在解决方案下建一个MVC4项目,.NET选4.5,修改根目录以及Vie ...
- CentOS下安装R
R的Windows版本有直接的安装包,直接下载安装很方便,但是对于CentOS6以上,不能直接通过yum 安装R,需要自己编译. 1. 在编译之前,用yum安装各种软件 (1)安装gcc > y ...
- jq 写法
<!doctype html> <html> <head> <meta charset="utf-8"> <script sr ...
- 制作炫酷banner js插件,revolution
这是一款非常强大的内容切换插件,它基于jQuery,它充分响应,支持移动设备,支持手机触摸,键盘翻页:它内置幻灯.视频播放计时器,它拥有各种模式:自定义,自动响应,全屏:它有多种动画效果.3d效果.. ...
- java 常用concurrent类
1.CountDownLatch 它的作用主要是当多个(数量等于初始化CountDownLatch时count参数的值)线程到达了预期状态或完成预期工作时触发事件,其他线程可以等待这个事件来触发自己后 ...