Codeforces Round #498 (Div. 3) D. Two Strings Swaps(思维,多种情况讨论)

D. Two Strings Swaps

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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:

扫描二维码关注公众号,回复: 2885454 查看本文章
  • 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 movesto 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

Copy

7
abacaba
bacabaa

output

Copy

4

input

Copy

5
zcabd
dbacz

output

Copy

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 44preprocess 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).

题意:给你a和b两个字符串,a[i] 和b[i] 可以交换,a[i] 和a[n-1-i] 可以交换, b[i]和b[n-1-i] 可以交换,交换不记数,看a串最少改变多少一个字符,可以和b串相等;

思路:分类讨论 a[i] ,a[n-1-i] ,b[i],b[n-1-i],看看他们的情况;

当讨论时,一定要注意,四个数中有三个不相等的数,说明其中两个是相等的,

有一种是特殊的   a   a

                            b    c

这种情况,需要第一个串, 需要改变两个字符 ,其他三个不相等的情况都是因为改变一个字符,再经交换就可相等

题意上说明了要只能改变第一个串

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#include<map>
#define Max 100005

char a[Max];
char b[Max];
int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		scanf("%s%s",a,b);
		int tt[4];
		int sum = 0;	
		for(int i = 0;i<n/2;i++)
		{
			int sum1 = 1;
			map<int ,int> m;
			m.clear();
			int book[5] = {0};
			tt[0] = a[i] - 'a'+1;
			tt[1] = a[n-1-i] - 'a'+1;
			tt[2] = b[i] - 'a'+1;
			tt[3] = b[n-1-i] - 'a'+1;
			for(int j = 0;j<4;j++)
			{
				if(!m[tt[j]])
				{
					m[tt[j]] = sum1;
					book[sum1] = 1;
					sum1++;
				}
				else
				{
					book[m[tt[j]]]++;
				}
			}
			sum1--;
			
			if(sum1==1||sum1==2&&book[1]==2&&book[2]==2)
				continue;
			else if(sum1==2)
				sum++;
			else if(sum1==3)   // 当这四个字符有三个是不样的,说明其中两个是一样的
							   // 只有 这种情况 a a 这种情况改变两个,其他情况都是改变一个; 
							   //   			b c     因为只能改变 s1 这个字符串; 
												 
			{
				
				if(tt[0]==tt[1])
					sum+=2;
				else sum++;
			}
			else sum+=2;	
		}
		if(n%2&&a[n/2]!=b[n/2])
			sum++;
		printf("%d\n",sum); 
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/obsorb_knowledge/article/details/81626374