QOJ.ac

QOJ

时间限制: 3 s 内存限制: 1024 MB 总分: 100 可 Hack ✓

#18671. 兩倍

统计

明具開發了次世代聊天應用程式 ChatChatA!

在發布 ChatChatA 後,一個嚴重的錯誤報告飛到了明具面前!在輸入訊息的過程中,如果訊息的最後一個字元是某個特定字元,輸入某個特定字元時,訊息會變成「兩倍」!

準確地解釋,「兩倍」的定義如下:

  • 設當前正在輸入的訊息為 $M$,$M$ 的最後一個字元為 $s$,將要輸入的字元為 $c$。
  • 令 $D$ 為會使訊息變成「兩倍」的 $(s, c)$ 配對集合。
    • 若 $(s, c) \in D$,則輸入 $c$ 時,$M$ 會變成 $M + M + c$,而不是 $M + c$!
    • 若 $(s, c) \notin D$,則輸入 $c$ 時,$M$ 會變成 $M + c$。
  • 若 $M$ 為空字串,則 $M$ 不會變成「兩倍」。

ChatChatA 的使用者具明想知道輸入字串 $T$ 所需的最少輸入次數。

當前訊息 $M$ 從空字串開始,每次輸入可以是以下兩種操作之一:

  • 在 $M$ 中加入字元 $c$。根據上述條件,訊息可能會變成「兩倍」。
  • 刪除 $M$ 的最後一個字元。僅當 $M$ 不是空字串時才能執行此操作。

請幫具明求出將當前訊息 $M$ 變成 $T$ 所需的最少輸入次數。

輸入格式

第一行輸入 $N$,表示 $D$ 的大小。($1 \le N \le 676 = 26^2$)

第二行輸入長度為 $N$ 的英文小寫字串 $S$,第三行輸入長度為 $N$ 的英文小寫字串 $C$。

若 $S$ 的第 $i$ 個字元為 $S_i$,$C$ 的第 $i$ 個字元為 $C_i$,則 $D = \{(S_i, C_i) : 1 \le i \le N\}$。

$|D| = N$。也就是說,不會出現相同的 $(S_i, C_i)$ 配對。

第四行輸入由英文小寫字母組成的字串 $T$。($1 \le |T| \le 500\,000$)

輸出格式

第一行輸出將目前訊息變成 $T$ 所需的最少輸入次數。

若無法將目前訊息變成 $T$,則輸出 $-1$。

範例

輸入格式 1

1
t
a
chatchata

輸出格式 1

5

輸入格式 2

2
ct
ha
chatchata

輸出格式 2

-1

輸入格式 3

2
af
bd
aafaaafaafaaafd

輸出格式 3

8

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.