Codeforces Round #336 (Div. 2)B. Hamming Distance Sum

Hamming Distance(字符串模拟) 题目描述 In information theory, the Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different. In other words, it measures the minimu... 阅读详情
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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|.

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 test(s)
input
01
00111
output
3
input
0011
0110
output
2
Note

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

        对于  "0011" , "0110"   价值就是  |0 - 0| + |0 - 1| + |1 - 1| + |1 - 0| = 0 + 1 + 0 + 1 = 2.

    a的长度严格小于等于b,a从b其实对应位置开始从右移到a,b末尾位置对应,问你在这一个过程中 价值是多少

情况一:
如果两个字符串长度一样,可以直接求。
情况二:
如果两个字符串长度不一样。比如样例1.
模拟的话是
01
00111
第一步
01
00
结果为1

01
01
结果为0

01
11
结果为1

01
11
结果为1
所以答案为3

但是长度为200 000,模拟时间的复杂度为O(n^2)显然会超时。


我们可以转换为b[i]字符和a的一些字符的Hamming 。

通过枚举我们可以发现规律。

01

00111

  01

00111

    01

00111

      01

00111


b字符串的第一个位置对应a字符串的第一个字符。

b字符串的第二个位置对应a字符串的前两个字符。

b字符串的第三个位置对应a字符串的第两个字符。

b字符串的第四个位置对应a字符串的第两个字符。

b字符串的第五个位置对应a字符串的最后个字符。

对任意的
a[0] a[1] a[2] a[3] ... a[n]
b[0] b[1] b[2] b[3] ... b[m] (m>=n)
1 2 3 ... k ... k ... 3 2 1 (对应的个数k <= n, k可能只有一个)
代码实现
#include
         
          
#include
          
           
#include
           
            
#include
            
             
#include
             
              
#include
               #include
               
                 using namespace std; const int N = 200001; char a[N]; char b[N]; long long l[N];//1的前缀和 int main(void) { // freopen("output.txt","r",stdin); while(~scanf("%s%s", a+1, b+1)) { int i, j; memset(l, 0, sizeof(l)); int len1 = strlen(a+1); int len2 = strlen(b+1); long long ans = 0; if(len1 == len2)//如果两个字符串长度相等 { for(i = 1; i <= len1; i++) { ans += (a[i] != b[i]); } printf("%I64d\n", ans); continue; } for(i = 1; i <= len1; i++) { l[i] = l[i-1] + (a[i] == '1'); } int tmp = len2 - len1+1;//b字符串对应的最大值 if(tmp > len1) tmp = len1;//不会大于第一个字符串的长度 int temp = 1;//起点为1 int flag = 0;//标记是加还是减,0表示加,1表示减 for(i = 1; i <= len2; i++) { if(b[i] == '0') { int tm = i; if(tm > len1) tm = len1;//起始位置不会大于第一个字符串的长度 ans += l[tm] - l[tm-temp]; } else { int tm = i; if(tm > len1) tm = len1; ans += (tm - l[tm]) - (tm - temp - l[tm-temp]); } if(!flag) { if(temp < tmp) temp++; } else { temp--; } if(i == len2 - tmp) flag = 1; } printf("%I64d\n", ans); } return 0; } 
               
             
            
           
          
         

SFT看这一篇就够了!万字长文:监督微调(Supervised Fine-Tuning, SFT)技术全解析【从理论到实战】 大家好,我是大 F,深耕AI算法十余年,互联网大厂技术岗。分享AI算法干货、技术心得。欢迎关注,一起探索技术的无限可能! 阅读详情

相关推荐

基于OpenMV与双舵机云台的激光追踪系统实战解析

本文详细解析了基于OpenMV摄像头与双舵机云台的激光追踪系统设计与实现。从硬件选型、图像识别算法到PID控制逻辑,全面介绍了如何构建一个高效稳定的激光追踪系统,并分享了电子设计竞赛中的实战经验与调试技巧。该系统在智能玩具、安防监控及教学演示等领域具有广泛应用前景。

prometheus9mon的博客 269

日常记录

Hamming distance:In information theory, the Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different. In other words, it me

cs_lsl的博客 228

基于python获取网易云热门歌单及封面

先看一下图片背后的代码 非常好,里面歌单名字,id,以及封面图片都有了.在复制url的时候注意把/#删掉,要不然爬不了. import requests from lxml import etree url='https://music.163.com/discover/playlist' head={ 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Ch

ziyuan12345678的博客 540

ZZ Hamming distance

  Hamming distance   n information theory, the Hamming distance, named after Richard Hamming, is the number of positions in two strings of equal length for which the corresponding elements are diff...

dreaming架构师,充分践行,适当补充理论的一年 181

28-基于STM32单片机的便携式健康监测系统设计与实现(程序+原理图+元件清单)

本文详细介绍了基于STM32单片机的便携式健康监测系统设计与实现,该系统集成MAX30102光电传感器和MSP20压力传感器,实时监测心率、血氧和血压等关键生理指标。通过STM32F103C8T6主控芯片和HC-05蓝牙模块,实现数据采集、处理及无线传输,具有便携性高、操作简便等特点,适用于家庭健康监护和户外运动场景。

weixin_29191669的博客 427

一文解读DeepSeek训练数据集

本文重点以DeepSeek为例,详细介绍预训练数据集相关内容。大语言模型LLM(如DeepSeek、ChatGPT、文心一言等)的“智慧”源于它们学习的海量数据集。简单来说,数据集就是模型的“知识库”,通常由互联网公开的文本、图片、视频等。这些数据需满足两个特点:规模大和多样性。规模大是指数据量可达千亿级词汇,让模型学习复杂规律;多样性则涵盖多领域(如科技、文学)、多语言、多形式(对话、新闻),帮助模型适应不同场景。例如,训练数据中既有百科知识,也有日常对话,模型才能既解答专业问题,又能闲聊。

u013891230的博客 7107

Codeforces Round #336 (Div. 2) 608B Hamming Distance Sum(dp)

B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Genos needs your help. He was asked

GKHack's Blog 1034

通信原理与MATLAB(八):2PSK的调制解调

通信原理和MATLAB

菜yuan~的博客 3万+

Codeforces Round #336 (Div. 2) B. Hamming Distance Sum

B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Genos needs your help. He was asked to solve the fo

KeyboardPianist的博客 813

芒果YOLO11改进172:ARConv自适应旋转卷积,全网首发 | 即插即用:以增强其表征能力,从而准确检测方向物体,ICCV顶会论文idea

ARConv自适应旋转卷积,全网首发 | 即插即用:以增强其表征能力,从而准确检测方向物体,ICCV顶会论文idea

包括YOLOv5、YOLOv7、YOLOv8等模型改进 1538

2025_NIPS_Truthful Aggregation of LLMs with an Application to Online Advertising

大型语言模型(LLMs)生成内容的 revenue 变现是在线广告的下一个前沿领域。我们考虑这样一种场景:广告商旨在影响LLM的回复,而平台则寻求最大化广告商价值并确保用户满意度。核心挑战在于,广告商的偏好通常与用户偏好存在冲突,且广告商可能误报其偏好。为解决这一问题,我们提出MOSAIC——一种拍卖机制,它确保真实申报是广告商的占优策略,并使每个广告商的效用与其对社会福利的贡献保持一致。重要的是,该机制无需LLM微调或访问模型权重,且随着计算资源的增加,可证明收敛到最优微调LLM的输出。

c_cpp_csharp的专栏 85

Codeforces Round #336 (Div. 2) B. Hamming Distance Sum (预处理)

B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Genos needs your help. He was asked t

滴水穿石,慢慢积累,总会有成果 588

【基础-多选】HarmonyOS鸿蒙应用开发

1、HarmonyOS提供了基础的应用加固安全能力,包括混淆、加密和代码签名能力。正确2、可以通过ohpm uninstall 指令下载指定的三方库。错误3、支持模块化开发是指一个应用通常会包含多种功能,将不同的功能特性按模块来划分和管理是一种良好的设计方式。在开发过程中,我们可以将每个功能模块作为一个独立的Module进行开发,Module中可以包含源代码、资源文件、第三方库、配置文件等,每一个Module可以独立编译,实现特定的功能。这种模块化、松耦合的应用管理方式有助于应用的开发、维护与扩展。正确。

江拥羡橙的博客 1万+

Codeforces Round #336 (Div. 2) B. Hamming Distance Sum

B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Genos needs your help. He was asked

RikkaTheWorld 650

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

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 ...

weixin_33788244的博客 96

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

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 problem by Saitama: The length of some stringsis denoted|s|. The Hamming distan...

weixin_30596343的博客 120

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

Codeforces Round #336 Hamming Distance Sum

题目: http://codeforces.com/contest/608/problem/B 字符串a和字符串b进行比较,以题目中的第一个样例为例,我刚开始的想法是拿01与00、01、11、11从左到右挨个比较,希望能找到一些规律,结果并没有。。。 其实,如果我们能从整个比较过程来看这个问题,整个过程就没有那么难。题目要求的东西,其实就是a字符串和b字符串子串每次比较的不同的个数的总和,当...

weixin_30856965的博客 93

Educational Codeforces Round 1 A. Tricky Sum

A. Tricky Sum time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output In this problem you are to calculate the sum o

ZZ_AC的博客 1091

Codeforces Round #336 (Div. 2)A. Saitama Destroys Hotel

A. Saitama Destroys Hotel time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Saitama accidentally destroyed a h

ZZ_AC的博客 1052

Codeforces Round #327 (Div. 2)A. Wizards' Duel

A. Wizards' Duel time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Harry Potter and He-Who-Must-Not-Be-Named

ZZ_AC的博客 850

Educational Codeforces Round 1 C. Nearest vectors

C. Nearest vectors time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given the set of vectors on the

ZZ_AC的博客 840

Codeforces Round #327 (Div. 2)B. Rebranding

B. Rebranding time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The name of one small but proud corporation c

ZZ_AC的博客 826

Codeforces Round #334 (Div. 2) C. Alternative Thinking

C. Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Kevin has just recevied his disappo

ZZ_AC的博客 793

Codeforces Round #320 (Div. 2)(B)

B. Finding Team Member time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output There is a programing contest named SnakeUp, 2n people wa

ZZ_AC的博客 776

Codeforces Round #323 (Div. 2)B. Robot's Task

B. Robot's Task time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Robot Doc is located in the hall, with n com

ZZ_AC的博客 754
上一篇: Codeforces Round #336 (Div. 2)A. Saitama Destroys Hotel
下一篇: assert()函数用法总结
zhaohuc
博客等级 码龄11年 5粉丝 110原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值