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 (1i,jn1≤i,j≤n), the cost of this operation is |ij||i−j|, that is, the absolute difference between ii and jj.
  • Select any arbitrary index ii (1in1≤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 (1n1061≤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 |13|=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 |23|=1|2−3|=1.


还是太直线思维了,老是想着用哪个减哪个,就没想到让答案一直增加:(

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <xfunctional>
#define ll long long
#define mod 998244353
using namespace std;
int dir[4][2] = { {0,1},{0,-1},{-1,0},{1,0} };
const int maxn = 1e5 + 5;
const long long inf = 0x7f7f7f7f7f7f7f7f;

int main()
{
    int n,res=0;
    cin >> n;
    string a, b;
    cin >> a >> b;
    int ans=0;
    for (int i = 0; i < a.size(); i++)
    {
        if (a[i] != b[i])
        {
            if (i + 1 < a.size() && a[i + 1] != b[i + 1] && a[i]!=a[i+1])
            {
                res++;
                i++;
            }
            else
            {
                res++;
            }
        }
    }
    cout << res;
}

猜你喜欢

转载自www.cnblogs.com/dealer/p/12341883.html