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 aa into bb.

Yousef has given you two binary strings, aa and bb, of the same length nn.

You are allowed to perform any of the following operations:

  • Choose a substring∗^{\text{∗}} in aa equal to 001\texttt{001} and replace it with 100\texttt{100}, or vice versa (i.e. 001→100\texttt{001} \rightarrow \texttt{100} or 100→001\texttt{100} \rightarrow \texttt {001}).
  • Choose a substring in aa equal to 110\texttt{110} and replace it with 011\texttt{011}, or vice versa (i.e. 011→110\texttt{011} \rightarrow \texttt{110} or 110→011\texttt{110} \rightarrow \texttt {011}).

Your task is to determine the minimum number of operations required to transform string aa into string bb. If it is impossible to transform aa into bb using the given operations, output −1-1 instead.

∗^{\text{∗}}A string aa is a substring of a string bb if aa can be obtained from bb by deletion of several (possibly zero or all) characters from the beginning and several (possibly zero or all) characters from the end.

这是该问题的困难版本。在本版本中,你需要确定将 aa 变换为 bb 所需的最少操作次数。

Yousef 给你两个长度均为 nn 的二进制字符串 aa 和 bb。

你被允许执行以下任意一种操作:

  • 在 aa 中选择一个等于 001\texttt{001} 的子串,并将其替换为 100\texttt{100};或者执行相反操作(即 001→100\texttt{001} \rightarrow \texttt{100} 或 100→001\texttt{100} \rightarrow \texttt{001})。
  • 在 aa 中选择一个等于 110\texttt{110} 的子串,并将其替换为 011\texttt{011};或者执行相反操作(即 011→110\texttt{011} \rightarrow \texttt{110} 或 110→011\texttt{110} \rightarrow \texttt{011})。

你的任务是确定将字符串 aa 变换为字符串 bb 所需的最少操作次数。如果无法通过给定操作将 aa 变换为 bb,则输出 −1-1。

∗^{\text{∗}} 若字符串 aa 可通过从字符串 bb 的开头删除若干(可能为零个或全部)字符、并从结尾删除若干(可能为零个或全部)字符而得到,则称 aa 是 bb 的子串。

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains a single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the length of each string.

The second line of each test case contains a binary string aa (∣a∣=n|a| = n), consisting of only characters 0\texttt{0} and/or 1\texttt{1}.

The third line of each test case contains a binary string bb (∣b∣=n|b| = n), consisting of only characters 0\texttt{0} and/or 1\texttt{1}.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 每个字符串的长度。

每个测试用例的第二行包含一个二进制字符串 aa(∣a∣=n|a| = n),仅由字符 0\texttt{0} 和/或 1\texttt{1} 组成。

每个测试用例的第三行包含一个二进制字符串 bb(∣b∣=n|b| = n),仅由字符 0\texttt{0} 和/或 1\texttt{1} 组成。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output the minimum number of operations required to transform aa into bb. If it is impossible, output −1-1 instead.

对于每个测试用例,输出将 aa 变换为 bb 所需的最少操作次数。如果无法实现,则输出 −1-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]=100a[2, 4] = \texttt{100} and replace it with 001\texttt{001}. This takes exactly 11 operation.

In the second test case, it is impossible to transform aa into bb, so the answer is −1-1.

In the third test case, we can do the following in order:

  • 1\texttt{1}100{\color{blue}{\texttt{100}}}00\texttt{00} →\rightarrow 1\texttt{1}001{\color{blue}{\texttt{001}}}00\texttt{00}
  • 100\texttt{100}100{\color{blue}{\texttt{100}}} →\rightarrow 100\texttt{100}001{\color{blue}{\texttt{001}}}
  • 100{\color{blue}{\texttt{100}}}001\texttt{001} →\rightarrow 001{\color{blue}{\texttt{001}}}001\texttt{001}
  • 00\texttt{00}100{\color{blue}{\texttt{100}}}1\texttt{1} →\rightarrow 00\texttt{00}001{\color{blue}{\texttt{001}}}1\texttt{1}

This takes 44 operations. It can be shown that 44 is the minimum answer.

在第一个测试用例中,我们可以选择子串 a[2,4]=100a[2, 4] = \texttt{100} 并将其替换为 001\texttt{001}。这恰好需要 11 次操作。

在第二个测试用例中,无法将 aa 变换为 bb,因此答案为 −1-1。

在第三个测试用例中,我们可以按如下顺序进行操作:

  • 1\texttt{1}100{\color{blue}{\texttt{100}}}00\texttt{00} →\rightarrow 1\texttt{1}001{\color{blue}{\texttt{001}}}00\texttt{00}
  • 100\texttt{100}100{\color{blue}{\texttt{100}}} →\rightarrow 100\texttt{100}001{\color{blue}{\texttt{001}}}
  • 100{\color{blue}{\texttt{100}}}001\texttt{001} →\rightarrow 001{\color{blue}{\texttt{001}}}001\texttt{001}
  • 00\texttt{00}100{\color{blue}{\texttt{100}}}1\texttt{1} →\rightarrow 00\texttt{00}001{\color{blue}{\texttt{001}}}1\texttt{1}

这总共需要 44 次操作。可以证明 44 是最小可能的答案。

输入解题思路,AI测评打分。不知道怎么写?

首页