首页 > 其他 > 详细

codeforces 1231E Middle-Out

时间:2019-10-03 16:53:32      阅读:89      评论:0      收藏:0      [点我收藏+]
//思路:因为可以任意位置的字符移动到首或尾位
//所以只需每次枚举最大不需要移动的子串就好了
#include<bits/stdc++.h>
#define inf (0x3f3f3f3f)
using namespace std;
int main()
{
    ios::sync_with_stdio(false);    cin.tie(0),cout.tie(0);
    int q,n;    cin>>q;
    while(q--)
    {
        cin>>n;
        string stra,strb;
        cin>>stra>>strb;
        int ans = inf;
        int cur;
        for(int i=0;i!=n;++i)
        {
            cur = i;//strb的当前位置
            for(int j=0;j!=n;++j)
            {
                if(cur!=n&&strb[cur]==stra[j])
                    ++cur;
            }
            ans = min(ans,n - cur + i);
        }
        sort(stra.begin(),stra.end());
        sort(strb.begin(),strb.end());
        if(stra != strb)
        {
            cout<<-1<<‘\n‘;
            continue;
        }
        cout<<ans<<‘\n‘;
    }
}

codeforces 1231E Middle-Out

原文:https://www.cnblogs.com/newstartCY/p/11619954.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!