C. Equalize

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值