CodeForces - 1006D

CodeForces - 1006D

You are given two strings a and b consisting of lowercase English letters, both of length n. The characters of both strings have indices from 1 to n, inclusive.

You are allowed to do the following changes:

Choose any index i (1≤i≤n) and swap characters ai and bi;
Choose any index i (1≤i≤n) and swap characters ai and an−i+1;
Choose any index i (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 b) 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 a with another character. In other words, in a single preprocess move you can choose any index i (1≤i≤n), any character c 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 a and b 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 b or make any preprocess moves after the first change is made.

INPUT

The first line of the input contains one integer n (1≤n≤105) — the length of strings a and b.

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 a equal to string b with a sequence of changes from the list above.

题意

两字符串长度不超过1e5,字符串有3种操作,交换a[i] b[i],交换a[i] a[n-i-1],交换b[i] b[n-i-1];但是依旧存在进行这三种操作之后两字符串依旧不相等的情况,所以我们将字符串中的一些字符替换掉,输出最小需要替换的字符的个数。(先替换再交换)

思路

由题意可知,我们其实只用考虑四个位置的字符,即 a[i] b[i] a[n-i-1] b[n-i-1],考虑这四个位置的字符的相同与不同个数。

#include<bits/stdc++.h>
using namespace std;
char a[100005];
char b[100005];
int main()
{
    int n;
    int ans=0;
    cin>>n;
    scanf("%s %s",&a,&b);
    for(int i=0;i<n/2;i++)
    {
        map<char ,int >s;
        s[a[i]]++;
        s[a[n-i-1]]++;
        s[b[i]]++;
        s[b[n-i-1]]++;
      if(s.size()==4)
        ans+=2;
      else if(s.size()==2)
      {
          if(s[a[i]]==1||s[a[i]]==3)
            ans+=1;
      }
      else if(s.size()==3)
      {
          if(a[i]==a[n-i-1])
            ans+=2;
          else
            ans+=1;
      }
    }
    if(n%2!=0&&a[n/2]!=b[n/2])
        ans+=1;
    cout<<ans<<endl;
    return 0;
}

posted @ 2018-07-30 09:28  cifiyoo  阅读(205)  评论(0编辑  收藏  举报