QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#20818. 検索と置換

Statistics

Bessie は最新で最高のテキスト編集ソフト miV を使っています!大小の英字だけの入力文字列を、ある出力文字列に変えたいと考えています。一度のキー操作で、英字 $c_1$ のすべての出現を別の英字 $c_2$ に置換できます。例えば $\texttt{aAbBa}$ で $c_1$ を a、$c_2$ を B とすると $\texttt{BAbBB}$ になります。

忙しい牛 Bessie のため、$T$ ($1\le T\le 10$) 個の独立したテストケースごとに、入力から望む出力への最少キー操作数を求めてください。

入力

最初の行は独立したテストケース数 $T$。

続く $T$ 対の行は同じ長さの入力・出力文字列です。文字は A から Z または a から z で、すべての文字列の長さの総和は $10^5$ 以下です。

出力

各ケースに最少キー操作数、変換できなければ $-1$ を出力してください。

入出力例

入力 1

4
abc
abc
BBC
ABC
abc
bbc
ABCD
BACD

出力 1

0
-1
1
3

注記

最初は入力と出力が同じなので操作不要です。

2 番目は、一方の $\texttt{B}$ を $\texttt{A}$ に変えてもう一方を $\texttt{B}$ のままにはできないので不可能です。

3 番目は $\texttt{a}$ を $\texttt{b}$ に変えればよいです。

最後は $\texttt{ABCD} \rightarrow \texttt{EBCD} \rightarrow \texttt{EACD} \rightarrow \texttt{BACD}$ と変えられます。

小課題

  • 入力 2–6:各文字列の長さは $50$ 以下。
  • 入力 7–9:小文字 $\texttt{a}$ から $\texttt{e}$ だけ。
  • 入力 10–15:追加の制約なし。

問題作成:Benjamin Qi

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.