#P11361. [NOIP2024] 编辑字符串
[NOIP2024] 编辑字符串
Description
Xiao M has two binary strings and of length (character set ).
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 () satisfying . 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 or , 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 , representing the number of test cases.
This is followed by test cases, each formatted as follows:
- The first line contains an integer , the length of the strings.
- The second line contains a binary string of length .
- The third line contains a binary string of length .
- The fourth line contains a binary string of length , where means is swappable, and means it is not.
- The fifth line contains a binary string of length , where means is swappable, and 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, (positions 4 and 6 are non-swappable), and (positions 2 and 5 are non-swappable).
One possible sequence of operations:
- Swap and to get .
- Swap and to get .
- Swap and to get .
Now, the first 4 positions of and 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 -th test case () satisfies the constraints described for test point in the data range.
Data Range
For all test data, it is guaranteed that: , .
| Test Case Number | Special Property | |
|---|---|---|
| 1∼4 | 10 | None |
| 5,6 | A | |
| 7,8 | ||
| 9,10 | B | |
| 11,12 | ||
| 13,14 | C | |
| 15,16 | ||
| 17,18 | None | |
| 19,20 |
- Special Property A: All characters in are the same.
- Special Property B: .
- Special Property C: Exactly one '0' exists in each of and .
Translated by DeepSeek R1
京公网安备 11011102002149号