CF2254C2.Marenol (hard version)
普及-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This is the hard version of the problem. In this version, you are asked to determine the minimum number of operations to transform a into b.
Yousef has given you two binary strings, a and b, of the same length n.
You are allowed to perform any of the following operations:
- Choose a substring∗ in a equal to 001 and replace it with 100, or vice versa (i.e. 001→100 or 100→001).
- Choose a substring in a equal to 110 and replace it with 011, or vice versa (i.e. 011→110 or 110→011).
Your task is to determine the minimum number of operations required to transform string a into string b. If it is impossible to transform a into b using the given operations, output −1 instead.
∗A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly zero or all) characters from the beginning and several (possibly zero or all) characters from the end.
这是该问题的困难版本。在本版本中,你需要确定将 a 变换为 b 所需的最少操作次数。
Yousef 给你两个长度均为 n 的二进制字符串 a 和 b。
你被允许执行以下任意一种操作:
- 在 a 中选择一个等于 001 的子串,并将其替换为 100;或者执行相反操作(即 001→100 或 100→001)。
- 在 a 中选择一个等于 110 的子串,并将其替换为 011;或者执行相反操作(即 011→110 或 110→011)。
你的任务是确定将字符串 a 变换为字符串 b 所需的最少操作次数。如果无法通过给定操作将 a 变换为 b,则输出 −1。
∗ 若字符串 a 可通过从字符串 b 的开头删除若干(可能为零个或全部)字符、并从结尾删除若干(可能为零个或全部)字符而得到,则称 a 是 b 的子串。
输入格式
The first line contains a single integer t (1≤t≤104) — the number of test cases.
The first line of each test case contains a single integer n (1≤n≤2⋅105) — the length of each string.
The second line of each test case contains a binary string a (∣a∣=n), consisting of only characters 0 and/or 1.
The third line of each test case contains a binary string b (∣b∣=n), consisting of only characters 0 and/or 1.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
第一行包含一个整数 t(1≤t≤104)—— 测试用例的数量。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105)—— 每个字符串的长度。
每个测试用例的第二行包含一个二进制字符串 a(∣a∣=n),仅由字符 0 和/或 1 组成。
每个测试用例的第三行包含一个二进制字符串 b(∣b∣=n),仅由字符 0 和/或 1 组成。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, output the minimum number of operations required to transform a into b. If it is impossible, output −1 instead.
对于每个测试用例,输出将 a 变换为 b 所需的最少操作次数。如果无法实现,则输出 −1。
输入输出样例
输入#1
5 4 0100 0001 4 0100 0010 6 110000 000011 8 10101010 10101010 5 01001 10010
输出#1
1 -1 4 0 3
说明/提示
In the first test case, we can choose the substring a[2,4]=100 and replace it with 001. This takes exactly 1 operation.
In the second test case, it is impossible to transform a into b, so the answer is −1.
In the third test case, we can do the following in order:
- 110000 → 100100
- 100100 → 100001
- 100001 → 001001
- 001001 → 000011
This takes 4 operations. It can be shown that 4 is the minimum answer.
在第一个测试用例中,我们可以选择子串 a[2,4]=100 并将其替换为 001。这恰好需要 1 次操作。
在第二个测试用例中,无法将 a 变换为 b,因此答案为 −1。
在第三个测试用例中,我们可以按如下顺序进行操作:
- 110000 → 100100
- 100100 → 100001
- 100001 → 001001
- 001001 → 000011
这总共需要 4 次操作。可以证明 4 是最小可能的答案。
输入解题思路,AI测评打分。不知道怎么写?