Two Strings Swaps CodeForces - 1006D

本文介绍了一种通过预处理操作和特定变换使两个字符串相等的算法。该算法首先确定最少需要改变源字符串中的多少个字符,之后可通过一系列规定变换进一步调整字符串直至两者完全相同。文章详细解释了算法的实现思路,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题:

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 1 to n, inclusive.

You are allowed to do the following changes:

  • Choose any index ii (1≤i≤n) and swap characters ai and bi;
  • Choose any index ii (1≤i≤n) and swap characters ai and an−i+1
  • Choose any index ii (1≤i≤n) and swap characters bi and bn−i+1

Note that if n is odd, you are formally allowed to swap a⌈n2⌉ with 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≤n), any character cc and set ai:=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≤105) — the length of strings aa and bb.

The second line contains the string a consisting of exactly n lowercase English letters.

The third line contains the string b consisting of exactly n 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 b 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:='b', a3:='c', a4:='a' and a5:='b'. Afterwards, a="bbcabba". Then we can obtain equal strings by the following sequence of changes: swap(a2,b2) and swap(a2,a6). There is no way to use fewer than 4 preprocess moves before a sequence of changes to make string equal, so the answer in this example is 4.

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(a2,a4).

大意:

给你两个等长的字符串啊a,b,问你最少要改变a中多少个字符,然后通过任意次上述三种变换,使两字符串相等。

思路:

去找a[i],a[n-1-i]和b[i],b[n-1-i]的关系,(1)如果四个都一样就不用换,(2)如果三个一样,只需要换一个,(3)如果两两一样就不用变,(4)只有两个一样,也要看情况,(5)如果四个都不一样就要换两个。

代码:

#include<map>
#define N 100100
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char a[N],b[N];
int n,sum;
int main()
{
    while(~scanf("%d",&n))
    {
        scanf("%s %s",a,b);
        sum=0;
        for (int i=0; i<n/2; i++)
        {
            map<char,int>m;
            m[a[i]]++;
            m[a[n-i-1]]++;
            m[b[i]]++;
            m[b[n-i-1]]++;
            if (m.size()==4)//四个都不一样
                sum+=2;
            else if(m.size()==3)//只有两个一样
            {
                sum+=1;
                if(a[i]==a[n-i-1])//a组的两个相等,就说明和b中的不等要改变两个
                    sum+=1;
            }
            else if(m.size()==2)//属于(2)(3)情况,
            {
                if(m[a[i]]!=2)//属于(2)
                    sum+=1;
            }
        }
        if (n%2&&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、付费专栏及课程。

余额充值