CF2242D.Two Digit Strings

普及/提高-

通过率:0%

时间限制:3.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given two strings aa and bb, consisting only of digits. In one operation, you can choose two adjacent characters in either of these two strings and replace them with their sum modulo 1010. For example, from the string 5724657246 you can obtain the following strings in one operation:

  • 57246→2246\mathbf{57}246 \rightarrow 2246;
  • 57246→59465\mathbf{72}46 \rightarrow 5946;
  • 57246→576657\mathbf{24}6 \rightarrow 5766;
  • 57246→5720572\mathbf{46} \rightarrow 5720;

Note that after such an operation, the length of the string always decreases by exactly 11.

You can perform any number of such operations (even zero). You have to make these strings equal (∣a∣=∣b∣|a| = |b| and ai=bia_i = b_i for all ii from 11 to ∣a∣|a|). Calculate the maximum possible length of the resulting equal strings.

给你两个仅由数字组成的字符串 aa 和 bb。在一次操作中,你可以任选这两个字符串中的一个,并在其内选择两个相邻字符,将它们替换为它们的模 10 和(即两数之和对 10 取余)。例如,从字符串 5724657246 出发,一次操作可得到以下字符串:

  • 57246→2246\mathbf{57}246 \rightarrow 2246;
  • 57246→59465\mathbf{72}46 \rightarrow 5946;
  • 57246→576657\mathbf{24}6 \rightarrow 5766;
  • 57246→5720572\mathbf{46} \rightarrow 5720;

注意:每次这样的操作后,字符串长度严格减少 1。

你可以执行任意多次(包括零次)此类操作。你的目标是使这两个字符串相等(即 ∣a∣=∣b∣|a| = |b| 且对所有 ii 从 11 到 ∣a∣|a| 均有 ai=bia_i = b_i)。请计算最终相等字符串可能达到的最大长度。

输入格式

The first line contains a single integer tt (1≤t≤2⋅1031 \le t \le 2 \cdot 10^3) — the number of test cases.

The first line of each test case contains a string aa (1≤∣a∣≤5⋅1031 \le |a| \le 5 \cdot 10^3), consisting only of digits.

The second line of each test case contains a string bb (1≤∣b∣≤5⋅1031 \le |b| \le 5 \cdot 10^3), consisting only of digits.

Additional constraint on the input: $\sum (|a| + |b|) $ across all test cases does not exceed 10410^4.

第一行包含一个整数 tt(1≤t≤2⋅1031 \le t \le 2 \cdot 10^3)—— 表示测试用例的数量。

每个测试用例的第一行包含一个字符串 aa(1≤∣a∣≤5⋅1031 \le |a| \le 5 \cdot 10^3),该字符串仅由数字组成。

每个测试用例的第二行包含一个字符串 bb(1≤∣b∣≤5⋅1031 \le |b| \le 5 \cdot 10^3),该字符串仅由数字组成。

输入的附加约束:所有测试用例中 ∣a∣+∣b∣|a| + |b| 的总和不超过 10410^4。

输出格式

For each test case, print one integer — the maximum possible length of the resulting equal strings. If you can't make the strings equal, print -1.

对于每个测试用例,输出一个整数——所得相等字符串的最大可能长度。如果无法使字符串相等,则输出 −1-1。

输入输出样例

  • 输入#1

    3
    5147
    44441
    2194
    5602
    123450
    012345

    输出#1

    2
    -1
    5

说明/提示

In the first example from the statement, the maximum length you can obtain is 2:

  • 5147→647→61\mathbf{51}47 \rightarrow 6\mathbf{47} \rightarrow 61;
  • 44441→8441→241→61\mathbf{44}441 \rightarrow \mathbf{84}41 \rightarrow \mathbf{24}1 \rightarrow 61.

In the second example from the statement, you can't make strings aa and bb equal, so the answer is -1.

In the third example from the statement, the maximum length you can obtain is 5:

  • 123450→123451234\mathbf{50} \rightarrow 12345;
  • 012345→12345\mathbf{01}2345 \rightarrow 12345.

在题目陈述的第一个示例中,你能得到的最大长度为 2:

  • 5147→647→61\mathbf{51}47 \rightarrow 6\mathbf{47} \rightarrow 61;
  • 44441→8441→241→61\mathbf{44}441 \rightarrow \mathbf{84}41 \rightarrow \mathbf{24}1 \rightarrow 61。

在题目陈述的第二个示例中,你无法使字符串 aa 和 bb 相等,因此答案为 −1-1。

在题目陈述的第三个示例中,你能得到的最大长度为 5:

  • 123450→123451234\mathbf{50} \rightarrow 12345;
  • 012345→12345\mathbf{01}2345 \rightarrow 12345。

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

首页