5508.Find and Replace

Time Limit: 1s Memory Limit: 256MB

Bessie is using the latest and greatest innovation in text-editing software, miV! She starts with an input string consisting solely of upper and lowercase English letters and wishes to transform it into some output string. With just one keystroke, miV allows her to replace all occurrences of one English letter $c_1$ in the string with another English letter $c_2$. For example, given the string $aAbBa$, if Bessie selects $c_1$ as 'a' and $c_2$ as 'B', the given string transforms into $BAbBB$.

Bessie is a busy cow, so for each of $T (1≤T≤10)$ independent test cases, output the minimum number of keystrokes required to transform her input string into her desired output string.

Input Format(From the terminal/stdin)

The first line contains $T$, the number of independent test cases.

The following $T$ pairs of lines contain an input and output string of equal length. All characters are upper or lowercase English letters (either A through Z or a through z). The sum of the lengths of all strings does not exceed $10^5$.

Output Format(To the terminal/stdout)

For each test case, output the minimum number of keystrokes required to change the input string into the output string, or $−1$ if it is impossible to do so.

Sample Input

Copy
4
abc
abc
BBC
ABC
abc
bbc
ABCD
BACD
 \n
   \n
   \n
   \n
   \n
   \n
   \n
    \n
    \n

Sample Output

Copy
0
-1
1
3
 \n
  \n
 \n
 \n

Hints

The first input string is the same as its output string, so no keystrokes are required.

The second input string cannot be changed into its output string because Bessie cannot change one 'B' to 'A' while keeping the other as 'B'.

The third input string can be changed into its output string by changing 'a' to 'b'.

The last input string can be changed into its output string like so: $ABCD→EBCD→EACD→BACD$.

Source: USACO 2023 Jan, Silver

Submit

请先 登录

© 2025 FAQs