C. Equalize

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.

Input

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

Output the minimum cost to make the string aa equal to bb.

Examples

input

Copy

3
100
001

output

Copy

2

input

Copy

4
0101
0011

output

Copy

1

Note

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.

Hint

本题题意为将两个长度为n的01字符串变成相等的,如果s1在相距为1的位置处(且不等于s2),交换他们的两个,sum+1,否则其它的不相等情况就加一。

#include <iostream>
#include <stack>using namespace std;const int maxn=1e6+4;
char s1[maxn],s2[maxn];
int main()
{int n,i,sum,k;cin>>n;cin>>s1>>s2;stack<int>q;sum=0;for(i=0;i<n;i++){if(s1[i]==s2[i])continue;if(s1[i]!=s2[i]){if(!q.empty()&&q.top()+1==i){k=q.top();if(s1[i]==s2[k]&&s1[k]==s2[i]){q.pop();sum+=1;}else q.push(i);}else if(!q.empty()&&q.top()+1<i)q.push(i);else if(q.empty())q.push(i);}}sum+=q.size();cout<<sum<<endl;return 0;
}

C. Equalize相关推荐

  1. 1037C. Equalize

    C. Equalize:题目 题意:a字符串变成b字符串,有两种方法,一种是选两个换位置,花费为abs[j-i],二是单独一个变,花费为1. 思路:如果两个需要变并且不一样挨在一起就可以省一点花费. ...

  2. 详解matlab均衡算法equalize()

    在matlab中,有一个常用的均衡器对象来均衡基带信号,它就是equalize(). 先来上一个例子看看具体的功效呗: 源码1: clc close all clear % Set up parame ...

  3. Equalize the Array(思维)

    题目链接: Equalize the Array 大致题意 给定一个长度为n的序列, 要求删除序列中尽可能少的元素, 使得剩余序列中出现的每一个元素数量相同. 解题思路 首先最终的序列, 一定是所有的 ...

  4. A. Equalize Prices Again

    滴答滴答---题目链接  A. Equalize Prices Again time limit per test 1 second memory limit per test 256 megabyt ...

  5. Codeforces Round #570 (Div. 3)B. Equalize Prices

    原题出处:http://codeforces.com/contest/1183/problem/B There are nn products in the shop. The price of th ...

  6. Equalize the Remainders(思维)

    Equalize the Remainders You are given an array consisting of n integers a1,a2,-,an, and a positive i ...

  7. Equalize the Array

    Equalize the Array 本题是来自于codeforces的一道1500分的题. 题目大意:给定一个数组,你需要让让他其中所有的元素出现的次数相等,途径是你可以删除任意个数字.问你需要最少 ...

  8. 可视化均衡插件-Acon Digital Equalize 2 v2.1.1 WiN-MAC

    VST, VST3, AAX, AU|32bit, 64bit|43.5M Equalize 2.0是一个功能全面的可视化均衡器,有着快速的操作流程和直观的操作界面,新版加入了两个新的滤波类型Tilt ...

  9. F. Equalize the Array【学习进度条2】

    F. Equalize the Array Tag:#map #思维 参考:F. Equalize the Array (map.思维) 因为只是用上一次结果的次数和个数,所以不需要每个次数都遍历,即 ...

最新文章

  1. python求平方根的代码_Python求平方根(附带源码)
  2. spring整合mybatis(入门级简单教程1)--在spring中配置c3p0,并成功测试
  3. JS的Document属性和方法
  4. Visual SVN 非常好的转贴
  5. 82. Leetcode 23. 合并K个升序链表 (排序)
  6. 基础网络爬虫(Web crawler)相关技术浅析
  7. C#中使用JavaScriptSerializer类实现序列化与反序列化
  8. Tensorflow GPU安装指南 (Ubuntu 16.04 anaconda cuda8.0 cuDNN6.0)
  9. 投资理财要趁早,基金风险是最小!
  10. npm如何设置淘宝镜像
  11. ios13.5.1降级_高并发系统下的降级如何实现
  12. 图片的 css sprite的应用思考
  13. keras + tensorflow —— 使用预训练模型
  14. python机器学习之决策树详解
  15. 简单好用的钉钉群消息助手
  16. PDF转CAD图纸,该如何转换呢?
  17. 计算机专业bs和cs,BS和CS的区别以及各自的优缺点
  18. Widows 和Linux 查看端口和杀掉进程的方法
  19. 几个数拼接生成最大数(java实现)
  20. 【STM32】STM32F103C8T6+nrf24l01收发示例

热门文章

  1. Telegram普通账号定时发布信息、签到
  2. 如何进行用户行为分析
  3. js动态修改表格数据
  4. 终于给自己买了台电脑
  5. 微信公众号历史数据采集和推文监控
  6. 网易传媒数据指标体系搭建实战!
  7. 谷歌浏览器安全证书不受信任_windows7系统下谷歌浏览器提示该网站的安全证书不受信任如何解决...
  8. 每天5分钟玩转K8S (看书笔记)
  9. 走进小程序【八】微信小程序中使用【Vant组件库】
  10. 用mg格式制作产品动画有什么优势?