关于前缀和,A - Hamming Distance Sum
前缀和思想
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|.
Examples
01
00111
3
0011
0110
2
https://blog.csdn.net/chen_yuazzy/article/details/77074410
根本思想请看https://www.cnblogs.com/mrclr/p/8423136.html
#include<cstdio>
#include<cstring>
#define m 300000
using namespace std;
char a[m];
char b[m];
int pre0[m];
int pre1[m];
int main()
{
int len1,len2,i;
long long s=0;
gets(a);
gets(b);
len1=strlen(a);
len2=strlen(b);
for(i=0;i<len2;i++)
{
if(b[i]=='0') {
pre0[i]=pre0[i-1]+1;
pre1[i]=pre1[i-1];
}
else if(b[i]=='1') {
pre1[i]=pre1[i-1]+1;
pre0[i]=pre0[i-1];
}
}
for(i=0;i<len1;i++)
{
if(a[i]=='0')
{
s+=pre1[i+len2-len1]-pre1[i-1];
}
else if(a[i]=='1')
{
s+=pre0[i+len2-len1]-pre0[i-1];
}
}
printf("%lld",s);
return 0;
}
关于前缀和,A - 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 Round #336 (Div. 2)B. Hamming Distance Sum 前缀和
B. Hamming Distance Sum 题目连接: http://www.codeforces.com/contest/608/problem/A Description Genos need ...
- Codeforces 608B. Hamming Distance Sum 模拟
B. Hamming Distance Sum time limit per test: 2 seconds memory limit per test:256 megabytes input: st ...
- 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 608 B. Hamming Distance Sum-前缀和
B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabytes input ...
- hdu 4712 Hamming Distance 随机
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- hdu 4712 Hamming Distance(随机函数暴力)
http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...
随机推荐
- mysql存储引擎选择(转)
MySQL 的存储引擎可能是所有关系型数据库产品中最具有特色的了,不仅可以同时使用多种存储引擎,而且每种存储引擎和MySQL之间使用插件方式这种非常松的耦合关系.由于各存储引擎功能特性差异较大,这篇文 ...
- MultipartFile 多文件上传的应用
公司的项目很多地方要用到文件上传,以前的上传主要是用apache的fileupload ,使用的感受并不太好.今天试了试spring的MultipartFile,感觉还不错,封装的比较简洁. 当然,中 ...
- JQuery遍历,find()和each()方法
find()方法 jquery选择器非常强大,利用css的命名规约,可以更快更方便的找出想要的元素. 比如: $("#id") $("#"+"id&q ...
- SQLServer 2008R2 清理日志文件
设置数据库为简单模式 2.收缩日志文件 3.恢复数据库为完整模式
- c#cookie读取写入操作
public static void SetCookie(string cname, string value, int effective) { HttpCookie cookie = new Ht ...
- 【WebSocket No.1】实现服务端webSocket连接通讯
前言 现阶段socket通信使用TCP.UDP协议,其中TCP协议相对来说比较安全稳定!本文也是来讲解TCP为主(恕在下学艺不精). 下面是个人理解的tcp/ip进行通讯之间的三次握手! 1.客户端先 ...
- 【转】Git 教程之协同开发
前面我们已经介绍过远程仓库的相关概念,不过那时并没有深入探讨,只是讲解了如何创建远程仓库以及推送最新工作成果到远程仓库,实际上远程仓库对于团队协同开发很重要,不仅仅是团队协同开发的基础,也是代码备份的 ...
- jQuery性能优化的一些参考建议
JQ3.1 文档下载地址:https://pan.baidu.com/s/1c2vMQdy 一.注意定义jQuery变量的时候添加var关键字 这个不仅仅是JQ,在JS中都是必须的 二.如果有多个变量 ...
- 20个Chrome DevTools调试技巧
译者按: Chrome DevTools很强大,甚至可以替代IDE了! 原文: Art of debugging with Chrome DevTools 译者: Fundebug 为了保证可读性,本 ...
- java 多线程简单例子
实现线程的方式是一,继承Thread类,重写父类的run()方法 二,实现接口Runnable中的run()方法. 下面是简单的例子 例子1:银行存取钱问题 package com.direct.de ...