Middle-Out【codeforces 1231E】(字符串匹配问题)

E. Middle-Out
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The problem was inspired by Pied Piper story. After a challenge from Hooli's compression competitor Nucleus, Richard pulled an all-nighter to invent a new approach to compression: middle-out.

You are given two strings ss and tt of the same length nn. Their characters are numbered from 11 to nn from left to right (i.e. from the beginning to the end).

In a single move you can do the following sequence of actions:

  • choose any valid index ii (1in1≤i≤n),
  • move the ii-th character of ss from its position to the beginning of the string or move the ii-th character of ss from its position to the end of the string.

Note, that the moves don't change the length of the string ss. You can apply a move only to the string ss.

For example, if s=s="test" in one move you can obtain:

  • if i=1i=1 and you move to the beginning, then the result is "test" (the string doesn't change),
  • if i=2i=2 and you move to the beginning, then the result is "etst",
  • if i=3i=3 and you move to the beginning, then the result is "stet",
  • if i=4i=4 and you move to the beginning, then the result is "ttes",
  • if i=1i=1 and you move to the end, then the result is "estt",
  • if i=2i=2 and you move to the end, then the result is "tste",
  • if i=3i=3 and you move to the end, then the result is "tets",
  • if i=4i=4 and you move to the end, then the result is "test" (the string doesn't change).

You want to make the string ss equal to the string tt. What is the minimum number of moves you need? If it is impossible to transform ss to tt, print -1.

Input

The first line contains integer qq (1q1001≤q≤100) — the number of independent test cases in the input.

Each test case is given in three lines. The first line of a test case contains nn (1n1001≤n≤100) — the length of the strings ss and tt. The second line contains ss, the third line contains tt. Both strings ss and tt have length nn and contain only lowercase Latin letters.

There are no constraints on the sum of nn in the test (i.e. the input with q=100q=100 and all n=100n=100 is allowed).

Output

For every test print minimum possible number of moves, which are needed to transform ss into tt, or -1, if it is impossible to do.

Examples
input
Copy
3
9
iredppipe
piedpiper
4
estt
test
4
tste
test
output
Copy
2
1
2
input
Copy
4
1
a
z
5
adhas
dasha
5
aashd
dasha
5
aahsd
dasha
output
Copy
-1
2
2
3
Note

In the first example, the moves in one of the optimal answers are:

  • for the first test case s=s="iredppipe", t=t="piedpiper": "iredppipe" → "iedppiper" → "piedpiper";
  • for the second test case s=s="estt", t=t="test": "estt" → "test";
  • for the third test case s=s="tste", t=t="test": "tste" → "etst" → "test".

 

 

解题报告:题意解释就是给定两个长度为n的字符串s和t,其中对于s中的任意一个字符他都可以将该字符放到字符串首部或者尾部,问最小的操作次数是多少,可以实现s==t

首先要明确一点就是我们可以通过调整单个字符到字符串首尾,那么我们就可以直接去寻找最小的s和t的不同字符就可以

 

ac代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;

int main()
{
    int t;    
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        string a,b;
        cin>>a>>b;
        int ans=1e9;
        for(int i=0;i<n;i++)
        {
            int k=i;
            for(int j=0;j<n;j++)
            {
                if(k<n&&a[j]==b[k]) k++;
            }//就是相当于我每次都以一个新的起点去枚举b,然后对于这个位置会有一个变成a的最小修改值
            //只要是有一部分相同的字符了,我就可以通过将某一位置的字符进行前/后置转换成想要的a 
            ans=min(ans,n-k+i);
            //    cout<<ans<<endl;
        }
//        cout<<a<<" "<<b<<endl;
        sort(a.begin(),a.end());
        sort(b.begin(),b.end());
//        cout<<a<<" "<<b<<endl;
        if(a!=b)
        {
            printf("-1\n");
            continue;
        }
        printf("%d\n",ans);
    }
}

 

 

转载于:https://www.cnblogs.com/Spring-Onion/p/11593467.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值