CF1701E.Text Editor

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You wanted to write a text tt consisting of mm lowercase Latin letters. But instead, you have written a text ss consisting of nn lowercase Latin letters, and now you want to fix it by obtaining the text tt from the text ss .

Initially, the cursor of your text editor is at the end of the text ss (after its last character). In one move, you can do one of the following actions:

  • press the "left" button, so the cursor is moved to the left by one position (or does nothing if it is pointing at the beginning of the text, i. e. before its first character);
  • press the "right" button, so the cursor is moved to the right by one position (or does nothing if it is pointing at the end of the text, i. e. after its last character);
  • press the "home" button, so the cursor is moved to the beginning of the text (before the first character of the text);
  • press the "end" button, so the cursor is moved to the end of the text (after the last character of the text);
  • press the "backspace" button, so the character before the cursor is removed from the text (if there is no such character, nothing happens).

Your task is to calculate the minimum number of moves required to obtain the text tt from the text ss using the given set of actions, or determine it is impossible to obtain the text tt from the text ss .

You have to answer TT independent test cases.

输入格式

The first line of the input contains one integer TT ( 1T50001 \le T \le 5000 ) — the number of test cases. Then TT test cases follow.

The first line of the test case contains two integers nn and mm ( 1mn50001 \le m \le n \le 5000 ) — the length of ss and the length of tt , respectively.

The second line of the test case contains the string ss consisting of nn lowercase Latin letters.

The third line of the test case contains the string tt consisting of mm lowercase Latin letters.

It is guaranteed that the sum of nn over all test cases does not exceed 50005000 ( n5000\sum n \le 5000 ).

输出格式

For each test case, print one integer — the minimum number of moves required to obtain the text tt from the text ss using the given set of actions, or -1 if it is impossible to obtain the text tt from the text ss in the given test case.

输入输出样例

  • 输入#1

    6
    9 4
    aaaaaaaaa
    aaaa
    7 3
    abacaba
    aaa
    5 4
    aabcd
    abcd
    4 2
    abba
    bb
    6 4
    baraka
    baka
    8 7
    question
    problem

    输出#1

    5
    6
    3
    4
    4
    -1
首页