Educational Codeforces Round 131 (Rated for Div. 2)E

E. Text Editor

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

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

The first line of the test case contains two integers nn and mm (1≤m≤n≤50001≤m≤n≤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 (∑n≤5000∑n≤5000).

Output

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.

Example

input

Copy

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

output

Copy

5
6
3
4
4
-1

多次left和多次right以及多次back和一次home,枚举home的位置即可,最后看看中间最长的连续子串即可。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#define int long long
#define lowbit(x) x&(-x)
#define mp make_pair
#define rep(i,x,n) for(int i=x;i<=n;i++)
#define per(i,n,x) for(int i=n;i>=x;i--)
#define pii pair<int,int>
using namespace std;
const int INF=1e9;
const int mod=998244353;
const int maxn=5e3+5 ;
inline int read()
{
    int x=0,f=1;
    char c=getchar();
    while(c<'0'||c>'9')
    {
        if(c=='-') f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        x=x*10+(c-'0');
        c=getchar();
    }
    return x*f;
}
int T;
int a[maxn];
char c[maxn],s[maxn];
int f[maxn][maxn],pre[maxn],suf[maxn];
signed main()
{
    T=read();
    while(T--)
    {
        int ans=INF;
        int n=read(),m=read();
        scanf("%s",c+1);
        scanf("%s",s+1);
        rep(i,1,n) rep(j,1,m) f[i][j]=0;
        int pos=1;
        rep(i,1,n)
        {
            if(s[pos]==c[i]) pos++;
            pre[i]=pos-1;
        }
        if(pre[n]!=m)
        {
            cout<<"-1"<<endl;
            continue;
        }
        pos=m;
        per(i,n,1)
        {
            if(s[pos]==c[i]) pos--;
            suf[i]=m-pos;
        }
        pre[0]=suf[n+1]=0;
        rep(i,1,n)
        {
            rep(j,1,m)
            {
                if(c[i]==s[j]) f[i][j]=f[i-1][j-1]+1;
                if(pre[i-f[i][j]]<j-f[i][j]||suf[i+1]<m-j) continue;
                ans=min(ans,(n-i)+(2*(i-f[i][j])-(j-f[i][j]))+1);
                if(i==f[i][j]) ans=min(ans,n-f[i][j]);
            }
        }
        cout<<min(ans,n)<<endl;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值