QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 512 MB 總分: 70

#13396. 童话

统计

小 Fabijan 觉得看图画书有些无聊了,于是他决定读他的第一本童话书。不幸的是,Fabijan 经常会遇到让他感到害怕的单词。为了克服恐惧,他决定玩一个自己发明的游戏。

这个可怕的单词可以表示为一个由 $n$ 个小写字母组成的数组。游戏开始时,Fabijan 将手指放在数组的某个位置上,并在纸上写下该位置的字母。然后,他可以进行任意次数的以下两种移动之一:

  • 将手指移动到当前位置左侧或右侧相邻的一个位置(如果该位置存在)。接着,Fabijan 会将新位置的字母写在纸上,紧跟在最后一个写下的字母后面。
  • 将手指移动到与当前位置字母相同的任意位置。在这种情况下,Fabijan 不会在纸上写任何东西。

将手指从位置 $x$ 移动到位置 $y$ 需要花费 $|x - y|$ 秒。

如果在游戏结束时,纸上写着 Fabijan 最喜欢的单词,他就克服了对这个单词的恐惧。他希望尽快读完童话书,因此他请求你告诉他,克服对给定的可怕单词的恐惧所需的最少秒数。

输入格式

第一行包含两个整数 $n$ 和 $m$($1 \le n, m \le 300$)。

第二行包含 $n$ 个小写字母,表示让 Fabijan 感到害怕的单词。

第三行包含 $m$ 个小写字母,表示 Fabijan 最喜欢的单词。

输出格式

输出 Fabijan 在纸上写出他最喜欢的单词所需的最短时间(以秒为单位),如果无法做到,则输出 $-1$。

子任务

在价值 $20$ 分的测试数据中,让 Fabijan 感到害怕的单词中的字母两两不同。

样例

输入样例 1

2 2
wa
ac

输出样例 1

-1

输入样例 2

7 7
monolog
nogolom

输出样例 2

10

输入样例 3

14 5
niskoobrazovan
boook

输出样例 3

5

说明

样例 3 解释:

Fabijan 首先将手指放在位置 $7$ 并写下字母 'b'。然后他向左移动两次,每次写下字母 'o'。在下一步中,他将使用第二种移动方式移动到位置 $6$。最后,他将再次向左移动两次,并写下字母 'o' 和 'k'。他总共花费了 $5$ 秒,每次移动花费 $1$ 秒。

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.