codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1006D.md (2960B)


      1 Source: https://codeforces.com/problemset/problem/1006/D
      2 
      3 D. Two Strings Swaps
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 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.
     22 
     23 You are allowed to do the following changes:
     24 
     25 * Choose any index $i$ ($1 \le i \le n$) and swap characters $a_i$ and $b_i$;
     26 * Choose any index $i$ ($1 \le i \le n$) and swap characters $a_i$ and $a_{n - i + 1}$;
     27 * Choose any index $i$ ($1 \le i \le n$) and swap characters $b_i$ and $b_{n - i + 1}$.
     28 
     29 Note that if $n$ is odd, you are formally allowed to swap $a_{\lceil\frac{n}{2}\rceil}$ with $a_{\lceil\frac{n}{2}\rceil}$ (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.
     30 
     31 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.
     32 
     33 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 \le i \le n$), any character $c$ and set $a_i := c$.
     34 
     35 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.
     36 
     37 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.
     38 
     39 Input
     40 
     41 The first line of the input contains one integer $n$ ($1 \le n \le 10^5$) — the length of strings $a$ and $b$.
     42 
     43 The second line contains the string $a$ consisting of exactly $n$ lowercase English letters.
     44 
     45 The third line contains the string $b$ consisting of exactly $n$ lowercase English letters.
     46 
     47 Output
     48 
     49 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.
     50 
     51 Examples
     52 
     53 Input
     54 
     55 ```
     56 7  
     57 abacaba  
     58 bacabaa
     59 ```
     60 
     61 Output
     62 
     63 ```
     64 4
     65 ```
     66 
     67 Input
     68 
     69 ```
     70 5  
     71 zcabd  
     72 dbacz
     73 ```
     74 
     75 Output
     76 
     77 ```
     78 0
     79 ```
     80 
     81 Note
     82 
     83 In the first example preprocess moves are as follows: $a_1 := $'b', $a_3 := $'c', $a_4 := $'a' and $a_5:=$'b'. Afterwards, $a = $"bbcabba". Then we can obtain equal strings by the following sequence of changes: $swap(a_2, b_2)$ and $swap(a_2, a_6)$. 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$.
     84 
     85 In the second example no preprocess moves are required. We can use the following sequence of changes to make $a$ and $b$ equal: $swap(b_1, b_5)$, $swap(a_2, a_4)$.