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 ...
随机推荐
- Python - Django - ORM 操作数据
查询数据(查询管理员): app01/models.py 中定义的类,也就是创建的表 from django.db import models # 类必须继承 models.Model class A ...
- python的分支循环
知识内容: 1.if-else分支结构 2.while循环 3.for循环 4.循环结构综述 5.break和continue语句 一.if-else分支结构 1.单分支选择结构 if 表达式: 语句 ...
- python入门-WHILE循环
1 使用while循环 current_number= : print(current_number) current_number += 2 让用户选择退出 prompt = "tell ...
- OpenACC 异步计算
▶ 按照书上的例子,使用 async 导语实现主机与设备端的异步计算 ● 代码,非异步的代码只要将其中的 async 以及第 29 行删除即可 #include <stdio.h> #in ...
- hive 锁
HiveQL是一种SQL语言,但缺少udpate和insert类型操作时的行,列或者查询级别的锁支持,hadoop文件通常是一次写入(支持有限的文件追加功能),hadoop和hive都是多用户系统,锁 ...
- 自定义BeanUtils
package com.icil.booking.service.util; import java.math.BigDecimal; import java.text.ParseException; ...
- 拒绝网页被 iframe 嵌套
在响应头里加一个X-Frame-Options DENY:浏览器拒绝当前页面加载任何Frame页面 SAMEORIGIN:frame页面的地址只能为同源域名下的页面 ALLOW-FROM origin ...
- 使用seaborn制图(小提琴图)
import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns # 设置风格, ...
- delphi 加密 XOR
From http://www.delphigeist.com/2009/09/text-encryption-with-xor.html Text encryption with XOR Ev ...
- python global nonlocal
global: 方法之外在modual中的变量定义为全局变量.方法内的变量为局部变量. 一般情况下,全局变量可以被使用,但是不应该被修改,不然会报错. 不过一般不建议对全局变量做修改,如果有多个方法都 ...