Two Strings Swaps

本文介绍了一道编程题目,任务是通过预处理操作最小化字符串修改次数,使得两个字符串能通过特定的字符交换规则变得相等。文章详细解释了题目的背景、解决思路及核心算法,并附带实现代码。

You are given two strings aa and bb consisting of lowercase English letters, both of length nn. The characters of both strings have indices from 11 to nn, inclusive.

You are allowed to do the following changes:

  • Choose any index ii (1≤i≤n1≤i≤n) and swap characters aiai and bibi;
  • Choose any index ii (1≤i≤n1≤i≤n) and swap characters aiai and an−i+1an−i+1;
  • Choose any index ii (1≤i≤n1≤i≤n) and swap characters bibi and bn−i+1bn−i+1.

Note that if nn is odd, you are formally allowed to swap a⌈n2⌉a⌈n2⌉ with a⌈n2⌉a⌈n2⌉ (and the same with the string bb) but this move is useless. Also you can swap two equal characters but this operation is useless as well.

You have to make these strings equal by applying any number of changes described above, in any order. But it is obvious that it may be impossible to make two strings equal by these swaps.

In one preprocess move you can replace a character in aa with another character. In other words, in a single preprocess move you can choose any index ii (1≤i≤n1≤i≤n), any character cc and set ai:=cai:=c.

Your task is to find the minimum number of preprocess moves to apply in such a way that after them you can make strings aa and bb equal by applying some number of changes described in the list above.

Note that the number of changes you make after the preprocess moves does not matter. Also note that you cannot apply preprocess moves to the string bb or make any preprocess moves after the first change is made.

Input

The first line of the input contains one integer nn(1≤n≤1051≤n≤105) — the length of strings aa and bb.

The second line contains the string aa consisting of exactly nn lowercase English letters.

The third line contains the string bb consisting of exactly nn lowercase English letters.

Output

Print a single integer — the minimum number of preprocess moves to apply before changes, so that it is possible to make the string aa equal to string bb with a sequence of changes from the list above.

Examples

Input

7
abacaba
bacabaa

Output

4

Input

5
zcabd
dbacz

Output

0

Note

In the first example preprocess moves are as follows: a1:=a1:='b', a3:=a3:='c', a4:=a4:='a' and a5:=a5:='b'. Afterwards, a=a="bbcabba". Then we can obtain equal strings by the following sequence of changes: swap(a2,b2)swap(a2,b2) and swap(a2,a6)swap(a2,a6). There is no way to use fewer than 44 preprocess moves before a sequence of changes to make string equal, so the answer in this example is 44.

In the second example no preprocess moves are required. We can use the following sequence of changes to make aa and bb equal: swap(b1,b5)swap(b1,b5), swap(a2,a4)swap(a2,a4).

题意:给出一个n,代表字符串的长度,下面两行是两个字符串a,b,可以进行   a[i]和a[n-i-1]交换  或  b[i]和b[n-i-1]交换  或a[i]和b[i]交换,可以通过改变字符串a中的字符使两个字符串变为相同。求刚开始最少对 a进行修改多少个字符才能使两个字符串可以通过上述三种交换方式得到两个相同的字符串。

思路:由题可得,这四个可交换的字符是可以到任何位置的,for环从0到n/2,每次取a的首尾和b的首尾存在map容器中,然后根据map容器中的字符数量分为不同的情况(用1 2 3表示)。

一. 如果容器大小为1,则这种情况a[i]=b[i],a[n-i-1]=b[n-i-1],不需要更改字符。

. 如果map容器大小为2,可能是两对两对相等或者其中三个相等,每种情况和对应的更改个数如下:

 1    1              1     2               1       2           1       1           1        1           1      2           2      1

 2    2              1     2               2       1           1       2           2        1            1      1          1      1

  0个                  0个                   0个                 1个                 1个                 1个               1个

跟据这几种情况对应的解,可得后几种情况需更换一位,前几种不用gen更换。

. 如果map容器大小为3,则有其中两个相等,每种情况对应的更改个数如下:

1      1            1      2               1        2              2       1           2      1           2       3

2      3            1      3               3        1              1       3           3       1          1       1

   2个                 1个                    1个                   1个               1个                   1个

根据这几种情况,可以得出结论,当a[i]=a[n-1-i]时,更换两个,否则更换一个。

. 如果map容器大小为4,则更换两个。

代码如下:

#include<stdio.h>
#include<string.h>
#include<map>
#include<algorithm>  //只能改变a中字符
using namespace std;
char a[100010],b[100010];
int main()
{
    int n,i,j,sum=0;
    scanf("%d",&n);
    scanf("%s%s",a,b);
    for(i=0; i<n/2; i++)
    {
        map<char,int>v;
        v[a[i]]++;
        v[a[n-i-1]]++;
        v[b[i]]++;
        v[b[n-i-1]]++;
        if(v.size()>=2)
        {
            if(v.size()==2)
            {
                if((a[i]==a[n-1-i]&&a[i]==b[i])||(a[i]==a[n-i-1]&&a[n-1-i]==b[n-1-i])||(a[i]==b[i]&&b[i]==b[n-1-i])||(a[n-1-i]==b[n-1-i]&&b[i]==b[n-1-i]))
                    sum++;
            }
            else if(v.size()==3)
            {
                if(a[i]==a[n-i-1])
                    sum=sum+2;
                else
                    sum++;
            }
            else
                sum=sum+2;
        }
    }
    if(n%2==1)
    {
        if(a[n/2]!=b[n/2])
            sum++;
    }
    printf("%d\n",sum);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值