Codeforces 608B. Hamming Distance Sum 模拟
2 seconds
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 is1 + 0 + 1 + 1 = 3.
The second sample case is described in the statement.
题意:输入两个只包含1和0的字符串a,b(1≤|a| ≤ |b| ≤ 200 000)。a与b中所有相等长度的连续子串进行比较,结果为每位数差的绝对值之和。输出所有结果的和。
样例1:a是01,b是00111;01与00(b中的第1位和第2位)比较,01与01(b中的第2位和第3位)比较,01与11(b中的第3位和第4位)比较,01与11(b中的第4位和第5位)比较。每一个比较的结果分别为1,0,1,1。输出结果的和3。
思路分析:a中的第1个数要和b中的第1,2,3,4个数进行比较,a中的第2个数要和b中的第2,3,4,5个数进行比较。a中的每一个数都比较了4次,设字符串a的长度为lena,b的长度为lenb,那就是要比较compare=lenb-lena+1次。每一次比较只要统计当前区间 [ b[i],b[i+compare])b为1的个数sum,如果a[i]=1,就加上compare-sum;如果a[i]=0,就加上sum。第一次sum的值就是for(i=0;i<compare;i++) if(b[i]==1) sum++;以后每次比较完之后就只要if(b[i]==1) sum--;if(b[i+compare]==1) sum++;
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
string a,b;
int na,nb,compare;
cin>>a>>b;
na=a.size();
nb=b.size();
compare=nb-na+;
int i;
__int64 sum=,ans=;
for(i=; i<compare; i++)
if(b[i]=='')sum++;
for(i=; i<na; i++)
{
if(a[i]=='') ans+=compare-sum;
else ans+=sum;
if(b[i]=='')sum--;
if(b[i+compare]=='')sum++;
}
cout<<ans<<endl;
}
Codeforces 608B. 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 (Div. 2) B. Hamming Distance Sum 计算答案贡献+前缀和
B. Hamming Distance Sum Genos needs your help. He was asked to solve the following programming pro ...
- 关于前缀和,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 Round #336 Hamming Distance Sum
题目: http://codeforces.com/contest/608/problem/B 字符串a和字符串b进行比较,以题目中的第一个样例为例,我刚开始的想法是拿01与00.01.11.11从左 ...
- codeforces 336 Div.2 B. Hamming Distance Sum
题目链接:http://codeforces.com/problemset/problem/608/B 题目意思:给出两个字符串 a 和 b,然后在b中找出跟 a 一样长度的连续子串,每一位进行求相减 ...
- Codeforces 280D k-Maximum Subsequence Sum [模拟费用流,线段树]
洛谷 Codeforces bzoj1,bzoj2 这可真是一道n倍经验题呢-- 思路 我首先想到了DP,然后矩阵,然后线段树,然后T飞-- 搜了题解之后发现是模拟费用流. 直接维护选k个子段时的最优 ...
- BZOJ 3836 Codeforces 280D k-Maximum Subsequence Sum (模拟费用流、线段树)
题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=3836 (Codeforces) http://codeforces.com ...
- Codeforces 608 B. Hamming Distance Sum-前缀和
B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabytes input ...
随机推荐
- sssp maven pom
pom <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.or ...
- leetcode227
class Solution { public: stack<int> OPD; stack<char> OPR; int calculate(int num1, int nu ...
- VB6 实现命令行调用时附着到原控制台
Public Declare Function AttachConsole Lib "kernel32.dll" (ByVal ProcessID As Integer) As B ...
- UI5-文档-4.16-Dialogs and Fragments
在这一步中,我们将进一步研究另一个可以用来组装视图的元素:the fragment. 片段是轻量级UI部件(UI子树),可以重用,但是没有任何控制器.这意味着,每当你想定义一个特定UI的一部分是跨多个 ...
- EF时间模糊查询
public List<Vote> SelectVoteByTime(string time) { return db.Vote.ToList().Where(x => x.V_Be ...
- Python3 impyla 连接 hiveserver2
简介: 接到一个任务,需要从 hive 中读取数据,生成报表. 于是找到了官方文档:https://cwiki.apache.org/confluence/display/Hive/Setting+U ...
- memcache命令
Command Description Example get 读取键值 get mykey set 设置新键值 set mykey 0 60 5 add 新增键值 add newkey 0 60 5 ...
- JAR命令使用
jar 命令详解 jar 是随 JDK 安装的,在 JDK 安装目录下的 bin 目录中,Windows 下文件名为 jar.exe,Linux 下文件名为 jar.它的运行需要用到 JDK 安装目录 ...
- Kotlin语言学习笔记(6)
运算符重载(Operator overloading) 一元运算符 Expression Translated to +a a.unaryPlus() -a a.unaryMinus() !a a.n ...
- springmvc框架的搭建
1引入jar包 jar包下载地址http://maven.springframework.org/release/org/ 以下是我引入的jar包 aopalliance-1.0.jaraspectj ...