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文件,并修改代码增加了通用性,点击这里查看 今天写小程序,有一个需求就是用户选择时间,然后我这边就要开始倒计时. 因为小程序的限制,所以直接选用时间选择器作为选择定时 ...
随机推荐
- 标准C库函数
标准库函数由15个头文件组成 1.math.h 1.1 绝对值函数 1.2 幂函数.开平方函数 1.3 指数函数.对数函数 1.5 三角函数 注意参数范围: 1.6 取整函数.取余函数 2.字符串处理 ...
- datetime模块常用功能小结
- 禅道-bug管理系统部署
官方下载: https://www.zentao.net/framework/zentao11.2-80109.html 官方使用说明:(含安装及使用说明,很详细) https://www.zenta ...
- webpack、npm、nginx常用命令
webpack命令:webpack --watch 监听变动并自动打包,简写-wwebpack -p --progress --color 压缩混淆脚本webpack -d 生成映射文件,告知那些模 ...
- (转)查询或修改iPhone的短信服务中心号码(iOS通用)
有些时候会有提示你自己的iPhone无法发送短信,原因是没有设置短信中心号码,下面就介绍一下如何设置iPhone的短信中心号码和iPhone查看短信中心号码的方法.(iOS通用) iPhone的设置短 ...
- C#存储过程中return参数
//1 连接字符串 string connectionString = "server=127.0.0.1;integrated security=true;database=MSPetSh ...
- AWS之SSH登录:使用 PuTTY 从 Windows 连接到 Linux 实例
使用 PuTTY 从 Windows 连接到 Linux 实例 启动您的实例之后,您可以连接到该实例,然后像使用您面前的计算机一样来使用它. 注意 启动实例后,需要几分钟准备好实例,以便您能连接到实例 ...
- 免安装版本Mysql配置
免安装版本mysql配置如下:(本人使用的是5.6.42) 1. 解压后将/bin目录配置在系统变量中 2. 在mysql目录下新建my.ini文件配置如下信息: [mysqld] basedir=D ...
- HTML中的GroupBox
<fieldset> <legend>用户登录</legend> <div class="box_a"> <label for ...
- 【Selenium-WebDriver自学】Selenium-IDE用户扩展(七)
==================================================================================================== ...