CF2242D.Two Digit Strings
普及/提高-
通过率:0%
时间限制:3.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given two strings a and b, 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 10. For example, from the string 57246 you can obtain the following strings in one operation:
- 57246→2246;
- 57246→5946;
- 57246→5766;
- 57246→5720;
Note that after such an operation, the length of the string always decreases by exactly 1.
You can perform any number of such operations (even zero). You have to make these strings equal (∣a∣=∣b∣ and ai=bi for all i from 1 to ∣a∣). Calculate the maximum possible length of the resulting equal strings.
给你两个仅由数字组成的字符串 a 和 b。在一次操作中,你可以任选这两个字符串中的一个,并在其内选择两个相邻字符,将它们替换为它们的模 10 和(即两数之和对 10 取余)。例如,从字符串 57246 出发,一次操作可得到以下字符串:
- 57246→2246;
- 57246→5946;
- 57246→5766;
- 57246→5720;
注意:每次这样的操作后,字符串长度严格减少 1。
你可以执行任意多次(包括零次)此类操作。你的目标是使这两个字符串相等(即 ∣a∣=∣b∣ 且对所有 i 从 1 到 ∣a∣ 均有 ai=bi)。请计算最终相等字符串可能达到的最大长度。
输入格式
The first line contains a single integer t (1≤t≤2⋅103) — the number of test cases.
The first line of each test case contains a string a (1≤∣a∣≤5⋅103), consisting only of digits.
The second line of each test case contains a string b (1≤∣b∣≤5⋅103), consisting only of digits.
Additional constraint on the input: $\sum (|a| + |b|) $ across all test cases does not exceed 104.
第一行包含一个整数 t(1≤t≤2⋅103)—— 表示测试用例的数量。
每个测试用例的第一行包含一个字符串 a(1≤∣a∣≤5⋅103),该字符串仅由数字组成。
每个测试用例的第二行包含一个字符串 b(1≤∣b∣≤5⋅103),该字符串仅由数字组成。
输入的附加约束:所有测试用例中 ∣a∣+∣b∣ 的总和不超过 104。
输出格式
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
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;
- 44441→8441→241→61.
In the second example from the statement, you can't make strings a and b equal, so the answer is -1.
In the third example from the statement, the maximum length you can obtain is 5:
- 123450→12345;
- 012345→12345.
在题目陈述的第一个示例中,你能得到的最大长度为 2:
- 5147→647→61;
- 44441→8441→241→61。
在题目陈述的第二个示例中,你无法使字符串 a 和 b 相等,因此答案为 −1。
在题目陈述的第三个示例中,你能得到的最大长度为 5:
- 123450→12345;
- 012345→12345。
输入解题思路,AI测评打分。不知道怎么写?