2017ecjtu-summer training #2 CodeForces 608B
2 seconds
256 megabytes
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.
题意: 定义两个字符串之间的距离是所有对应的每个字符相减绝对值的和, 给定a和b两个字符串, 求b中与a长度相同的所有子串与a字符串距离之和.
分析: 多写一些样例,就可以归纳出来, 其实就是a字符串中的第一个元素,分别 与b字符串中的第一个元素至第|b|-|a|+1个元素求距离的和, 再加上a字符串中的第2个元素,分别 与b字符串中的第2个元素至第|b|-|a|+2个元素求距离的和......直到a字符串中的第|a|个元素与b字符串中的第|a|个元素至第|b|个元素求距离的总和. 由于求距离很像求异或和, 因此可以先用两个数组分别保存b字符串的0个数的前缀和和1个数的前缀和, 遍历a字符串的时候 ,如果字符是1 ,那么求相应区间0的的个数, 如果字符是0 ,那么求相应区间1的的个数.
AC代码
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define maxn 222222
typedef long long ll;
char a[maxn],b[maxn];
int res1[maxn],res0[maxn];
int main()
{
while(~scanf("%s%s",a+,b+))
{
int la=strlen(a+),lb=strlen(b+);
res1[]=res0[]=;
for(int i=; i<=lb; i++)
{
if(b[i]=='')
{
res1[i]=res1[i-]+;
res0[i]=res0[i-];
}
else
{
res1[i]=res1[i-];
res0[i]=res0[i-]+;
}
}
ll ans=;
for(int i=; i<=la; i++)
if(a[i]=='')
ans+=res0[lb-(la-i)]-res0[i-];
else
ans+=res1[lb-(la-i)]-res1[i-];
printf("%I64d\n",ans);
}
return ;
}
2017ecjtu-summer training #2 CodeForces 608B的更多相关文章
- HZNU 2019 Summer training 6 -CodeForces - 622
A - Infinite Sequence CodeForces - 622A 题目大意:给你一个这样的数列1,1,2,1,2,3,1,2,3,4,1,2,3,4,5....就是从1~n排列(n++ ...
- 2017ecjtu-summer training #4 CodeForces 731C
C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- Codeforces 608B. Hamming Distance Sum 模拟
B. Hamming Distance Sum time limit per test: 2 seconds memory limit per test:256 megabytes input: st ...
- Codeforces Round #588 (Div. 2) D. Marcin and Training Camp(思维)
链接: https://codeforces.com/contest/1230/problem/D 题意: Marcin is a coach in his university. There are ...
- Codeforces Training S03E01泛做
http://codeforces.com/gym/101078 和ysy.方老师一起打的virtual 打的不是很好...下面按过题顺序放一下过的题的题(dai)解(ma). A 给两个1~n的排列 ...
- codeforces 519C. A and B and Team Training 解题报告
题目链接:http://codeforces.com/contest/519/problem/C 题目意思:给出 n 个 experienced participants 和 m 个 newbie ...
- codeforces 519C.. A and B and Team Training
C. A and B and Team Training time limit per test 1 second memory limit per test 256 megabytes input ...
- 【Codeforces 1132D】Stressful Training
Codeforces 1132 D 题意:给\(n\)个电脑的电量和耗电速度,你可以买一个充电器,它的充电速度是每秒\(v\)单位,\(v\)你自己定.问最小的\(v\)能使得在\(k\)秒内每秒给某 ...
- Codeforces 1132D - Stressful Training - [二分+贪心+优先队列]
题目链接:https://codeforces.com/contest/1132/problem/D 题意: 有 $n$ 个学生,他们的电脑有初始电量 $a[1 \sim n]$,他们的电脑每分钟会耗 ...
随机推荐
- 常规流(Normal flow)
连我自己把float和绝对定位,都称为脱离文档流,想想概念又不那么清晰,于是寻找了W3C资料来理解,才发觉不应该叫文档流. 资料 英文:https://www.w3.org/TR/CSS22/visu ...
- Python列表操作
自带帮助文档: >>> help(list) Help on class list in module builtins: class list(object) | list() - ...
- Node.js 蚕食计划(一)—— 模块化编程
众所周知,Node.js 的出现造就了全栈工程师,因为它让 JavaScript 的舞台从浏览器扩大到了服务端 而 Node.js 的强大也得益于它庞大的模块库,所以学习 Node.js 第一步还得从 ...
- PAGELATCH_x 等待--转载
转自出处:http://www.cnblogs.com/xwdreamer/archive/2012/08/30/2663232.html 0.参考文献 Microsoft SQL Server企业级 ...
- solr7.2安装实例,中文分词器
一.安装实例 1.创建实例目录 [root@node004]# mkdir -p /usr/local/solr/home/jonychen 2.复制实例相关配置文件 [root@node004]# ...
- 在阿里云 ECS 搭建 nginx https nodejs 环境(二、https)
在阿里云 ECS 搭建 nginx https nodejs 环境(二) 这次主要内容是 如何在 ubuntu 的nginx 下配置 二级域名. 一. 域名解析 首先你需要去到你的 域名服务商那边 进 ...
- Java学习笔记13(面向对象六:super)
在创建子类对象时,父类的构造方法会先执行,因为子类中所有构造方法的第一行有默认的隐式super();语句 注意:父类构造方法第一行也有隐式的super(); 所有类都有一个"祖宗类" ...
- ionic2 App搭建(三)
cmd命令提示框中进入项目文件夹 运行命令 ionic serve --lab 结构如下图 这里数据是没有接受到的,是因为跨域的问题,解决方案是谷歌浏览器配置跨域指令如下: 配置chrome浏览器允 ...
- ADO对SQL Server 2008数据库的基础操作
最近在学习ADO与数据库的相关知识,现在我将自己学到的东西整理写出来,也算是对学习的一种复习. 这篇文章主要说明如何遍历某台机器上所有的数据库服务,遍历某个服务中所有的数据库,遍历数据库中的所有表以及 ...
- Spring框架入门之Spring简介
一.Spring简介(由Rod Johnson创建的一个开源框架) Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿 ...