luogu#P16328. 夜曲

    ID: 16169 远端评测题 500ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>二分洛谷原创O2优化洛谷月赛双指针 two-pointer

夜曲

Problem Description

You are given two strings ss and tt of length nn, indexed from 11.

In one operation, you may freely choose a non-decreasing integer sequence pp of length nn with values in [1,n][1,n], satisfying pii1|p_i-i|\le 1. Then, simultaneously, set all sispis_i\gets s_{p_i}.

Find the minimum number of operations needed to transform ss into tt. If it is impossible, output 1-1.

Input Format

This problem contains multiple test cases.

The first line contains a positive integer TT, the number of test cases.

Then for each test case, the first line contains the string ss, and the second line contains the string tt.

Output Format

For each test case, output one integer per line, the answer.

11
aabbccc
abbbbbc
abbac
baabc
abcbcab
abbabbb
ababbbac
abaaabcc
aab
baa
ababaabababbb
baaabaababbba
abrcadabra
aaaabbbraa
aabababababababbababaab
abababaaabababababbabbb
azusa
aaaaa
anotheruniversity
inneroceanfantasy
breakplus
bbbbbpppp
2
-1
2
2
-1
3
3
1
2
-1
4

Hint

Sample Explanation

For the first test case, we can transform ss into tt as follows:

$$\texttt{aabbccc}\to\texttt{abbbbcc}\to\texttt{abbbbbc}$$

The corresponding sequences pp for the two operations are [1,3,4,4,4,5,6][1,3,4,4,4,5,6] and [1,2,3,4,5,5,7][1,2,3,4,5,5,7].

It is impossible to do this in a single operation, so the answer is 22.

Constraints

Let mm denote the size of the alphabet, that is, the strings only contain the first mm lowercase English letters.

Subtask\text{Subtask} n,nn,\sum n\le mm\le Special Property Score\text{Score}
11 2020 22 A 1515
22 5×1035\times 10^3 ^ None 3030
33 10610^6 2626 B 2525
44 ^ None 3030
  • Special Property A: It is guaranteed that sisi+1s_i\le s_{i+1} and titi+1t_i\le t_{i+1}.

  • Special Property B: It is guaranteed that tt only contains the character a.

For all data, it is guaranteed that 1T5×1031\le T\le 5\times 10^3, 1n,n1061\le n,\sum n\le 10^6, the strings contain only lowercase English letters, and s=t|s|=|t|.