1037C_ Equalize(字符串)
1 second
256 megabytes
standard input
standard output
You are given two binary strings aa and bb of the same length. You can perform the following two operations on the string aa:
- Swap any two bits at indices ii and jj respectively (1≤i,j≤n1≤i,j≤n), the cost of this operation is |i−j||i−j|, that is, the absolute difference between ii and jj.
- Select any arbitrary index ii (1≤i≤n1≤i≤n) and flip (change 00 to 11 or 11 to 00) the bit at this index. The cost of this operation is 11.
Find the minimum cost to make the string aa equal to bb. It is not allowed to modify string bb.
The first line contains a single integer nn (1≤n≤1061≤n≤106) — the length of the strings aa and bb.
The second and third lines contain strings aa and bb respectively.
Both strings aa and bb have length nn and contain only '0' and '1'.
Output the minimum cost to make the string aa equal to bb.
3
100
001
2
4
0101
0011
1
In the first example, one of the optimal solutions is to flip index 11 and index 33, the string aa changes in the following way: "100" →→ "000" →→"001". The cost is 1+1=21+1=2.
The other optimal solution is to swap bits and indices 11 and 33, the string aa changes then "100" →→ "001", the cost is also |1−3|=2|1−3|=2.
In the second example, the optimal solution is to swap bits at indices 22 and 33, the string aa changes as "0101" →→ "0011". The cost is |2−3|=1|2−3|=1.
题意:给你两个由二进制数表示的字符串a,b,要使a=b,并且花费最少,a可以进行如下两个操作,1:交换a字符串的两个的数,花费为|i-j| 2:改变第i个数,1变0,0变1,花费为1
分析:如果进行交换两个数的操作,那么当|i-j|>2的时候就不划算了。当|i-j|=1时,并且a[i]!=a[j],如果交换的话,花费为1,如果改变值得话,花费为2。其余情况均是改变值的时候最划算。所以当|i-j|=1,并且a[i]!=a[j]时,让a[j]=b[j]。其余每步都进行改变值花费为1的操作,即ans++。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int n;
char a[],b[];
while(~scanf("%d",&n))
{
scanf("%s",a+);
scanf("%s",b+);
int ans=;
for(int i=;i<=n;i++)
{
if(a[i]!=b[i])
{
if(a[i+]!=b[i+]&&a[i+]!=a[i])
a[i+]=b[i+];
ans++;
}
}
printf("%d\n",ans);
}
return ;
}
1037C_ Equalize(字符串)的更多相关文章
- Python高手之路【六】python基础之字符串格式化
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
- 测试一下StringBuffer和StringBuilder及字面常量拼接三种字符串的效率
之前一篇里写过字符串常用类的三种方式<java中的字符串相关知识整理>,只不过这个只是分析并不知道他们之间会有多大的区别,或者所谓的StringBuffer能提升多少拼接效率呢?为此写个简 ...
- java中的字符串相关知识整理
字符串为什么这么重要 写了多年java的开发应该对String不陌生,但是我却越发觉得它陌生.每学一门编程语言就会与字符串这个关键词打不少交道.看来它真的很重要. 字符串就是一系列的字符组合的串,如果 ...
- JavaScript 字符串实用常操纪要
JavaScript 字符串用于存储和处理文本.因此在编写 JS 代码之时她总如影随形,在你处理用户的输入数据的时候,在读取或设置 DOM 对象的属性时,在操作 Cookie 时,在转换各种不同 Da ...
- Java 字符串格式化详解
Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...
- Redis的简单动态字符串实现
Redis 没有直接使用 C 语言传统的字符串表示(以空字符结尾的字符数组,以下简称 C 字符串), 而是自己构建了一种名为简单动态字符串(simple dynamic string,sds)的抽象类 ...
- ASP.NET加密和解密数据库连接字符串
大家知道,在应用程序中进行数据库操作需要连接字符串,而如果没有连接字符串,我们就无法在应用程序中完成检索数据,创建数据等一系列的数据库操作.当有人想要获取你程序中的数据库信息,他首先看到的可能会是We ...
- Javascript正则对象方法与字符串正则方法总结
正则对象 var reg = new Regexp('abc','gi') var reg = /abc/ig 正则方法 test方法(测试某个字符串是否匹配) var str = 'abc123'; ...
- 微信小程序中利用时间选择器和js无计算实现定时器(将字符串或秒数转换成倒计时)
转载注明出处 改成了一个单独的js文件,并修改代码增加了通用性,点击这里查看 今天写小程序,有一个需求就是用户选择时间,然后我这边就要开始倒计时. 因为小程序的限制,所以直接选用时间选择器作为选择定时 ...
随机推荐
- centos7安装zabbix4.2
附zabbixdocker镜像地址 https://hub.docker.com/u/zabbix/ zabbix官方文档 https://www.zabbix.com/cn/download 1.关 ...
- ros6.44版本增加了测试出两台ROS之间的速度极限是多少
原文: https://wiki.mikrotik.com/wiki/Manual:Tools/Speed_Test [admin@MikroTik]] > /tool speed-test a ...
- Java中的Future相关
先上一个场景:假如你突然想做饭,但是没有厨具,也没有食材.网上购买厨具比较方便,食材去超市买更放心. 实现分析:在快递员送厨具的期间,我们肯定不会闲着,可以去超市买食材.所以,在主线程里面另起一个子线 ...
- T-SQL 事务
use StudentManager go declare @errorSum int --定义变量,用于累计事务执行过程中的错误 --初始化为0,即无错误 begin transaction beg ...
- 搞定queryString
querystring字面上的意思就是查询字符串,一般是对http请求所带的数据进行解析.querystring模块只提供4个方法: querystring.parse querystring.str ...
- redis作为mysql的缓存服务器(读写分离)
转自:https://www.iyunv.com/thread-52670-1-1.html 一.redis简介Redis是一个key-value存储系统.和Memcached类似,为了保证效率,数据 ...
- 第9章 应用层(5)_文件传输协议FTP
6. 文件传输协议FTP 6.1 FTP主动和被动模式 (1)FTP协议 ①与其他协议不同,FTP协议在客户端访问FTP服务器时需要建立两个TCP连接.一个用来传输FTP命令,一个用来传输数据. ②在 ...
- 使用LVS实现负载均衡原理及安装配置详解(转)
负载均衡集群是 load balance 集群的简写,翻译成中文就是负载均衡集群.常用的负载均衡开源软件有nginx.lvs.haproxy,商业的硬件负载均衡设备F5.Netscale.这里主要是学 ...
- 基于Boost的同步TCP通信
1:客户端 #include <iostream> #include <boost/asio.hpp> using namespace boost; using namespa ...
- 微信小程序学习笔记1--小程序的代码构成
最近打算学习一下微信小程序,看了微信公众平台的文档感觉还比较简单,就从这个方向重新找回学习的状态吧: 1.先了解一下小程序的代码构成: 创建项目后会看到四种后缀的文件: .json 后缀的 JSON ...