#P11361. Maximizing Matching Characters

    ID: 13437 Type: Default 1000ms 256MiB

Maximizing Matching Characters

Maximizing Matching Characters

Little M has two binary strings \( s_1 \) and \( s_2 \), each of length \( n \) and consisting of characters from the set \( \{0, 1\} \). Little M wants to make the two strings as similar as possible by maximizing the number of positions \( i \) (where \( 1 \leq i \leq n \)) such that \( s_{1,i} = s_{2,i} \).

To achieve this, Little M can use a string editing tool which allows swapping of two adjacent characters within a single string. However, to preserve the recognizability of the strings, some characters in each string are not allowed to participate in swaps. For the characters that are allowed to be swapped, any number of adjacent swaps may be performed, which means that these swappable characters can be arbitrarily rearranged within their positions.

In this problem, assume that all characters are swappable (i.e. there are no restrictions on swaps). Under this assumption, the order of characters in each string can be rearranged arbitrarily. Thus, the goal is to determine the maximum number of matching positions that can be obtained after arbitrarily reordering each string.

Since any reordering is allowed, the maximum number of matching positions is given by:

\[ \min\big(\#0(s_1),\;\#0(s_2)\big) + \min\big(\#1(s_1),\;\#1(s_2)\big), \]

where \(\#0(s)\) and \(\#1(s)\) denote the number of '0's and '1's in string \( s \), respectively.

Your task is to compute this maximum matching count.

inputFormat

The first line contains an integer \( n \) representing the length of the strings.

The second line contains the binary string \( s_1 \).

The third line contains the binary string \( s_2 \).

outputFormat

Output a single integer, representing the maximum possible number of matching positions between the two strings after performing allowed swaps.

sample

3
010
001
3