#P11361. [NOIP2024] 编辑字符串

[NOIP2024] 编辑字符串

Description

Xiao M has two binary strings s1s_1 and s2s_2 of length nn (character set {0,1}\{0, 1\}).

Xiao M wants to maximize the number of positions where the corresponding characters in the two strings are the same, i.e., maximize the count of indices ii (1in1 \leq i \leq n) satisfying s1,i=s2,is_{1,i} = s_{2,i}. To achieve this, Xiao M has a string editing tool that allows swapping two adjacent characters in a string. However, some characters in the strings are marked as non-swappable. Xiao M can perform multiple swap operations on s1s_1 or s2s_2, and swappable characters can be moved any number of times.

Now, Xiao M wants to know the maximum number of matching positions that can be achieved after using the editing tool.

Input Format

The problem contains multiple test cases.

The first line of input contains an integer TT, representing the number of test cases.

This is followed by TT test cases, each formatted as follows:

  • The first line contains an integer nn, the length of the strings.
  • The second line contains a binary string s1s_1 of length nn.
  • The third line contains a binary string s2s_2 of length nn.
  • The fourth line contains a binary string t1t_1 of length nn, where t1,i=1t_{1,i} = 1 means s1,is_{1,i} is swappable, and t1,i=0t_{1,i} = 0 means it is not.
  • The fifth line contains a binary string t2t_2 of length nn, where t2,i=1t_{2,i} = 1 means s2,is_{2,i} is swappable, and t2,i=0t_{2,i} = 0 means it is not.

Output Format

For each test case, output one line containing an integer, the maximum number of matching positions.

1
6
011101
111010
111010
101101
4

Hint

Explanation for Sample #1

Initially, s1=011101s_1 = \tt{011101} (positions 4 and 6 are non-swappable), and s2=111010s_2 = \tt{111010} (positions 2 and 5 are non-swappable).

One possible sequence of operations:

  1. Swap s1,1s_{1,1} and s1,2s_{1,2} to get s1=101101s_1 = \tt{101101}.
  2. Swap s1,2s_{1,2} and s1,3s_{1,3} to get s1=110101s_1 = \tt{110101}.
  3. Swap s2,3s_{2,3} and s2,4s_{2,4} to get s2=110110s_2 = \tt{110110}.

Now, the first 4 positions of s1s_1 and s2s_2 match. It can be proven that no better solution exists, so the answer is 4.

Explanation for Sample #2

See files edit/edit2.in and edit/edit2.ans in the attachment.

This sample contains 10 test cases, where the ii-th test case (1i101 \leq i \leq 10) satisfies the constraints described for test point 2i12i - 1 in the data range.

Data Range

For all test data, it is guaranteed that: 1T101 \leq T \leq 10, 1n1051 \leq n \leq 10^5.

Test Case Number nn \leq Special Property
1∼4 10 None
5,6 10310^3 A
7,8 10510^5
9,10 10310^3 B
11,12 10510^5
13,14 10310^3 C
15,16 10510^5
17,18 10310^3 None
19,20 10510^5
  • Special Property A: All characters in s1s_1 are the same.
  • Special Property B: t1=t2t_1 = t_2.
  • Special Property C: Exactly one '0' exists in each of t1t_1 and t2t_2.

Translated by DeepSeek R1