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

编辑距离与文本转换
该博客探讨了一种计算从字符串s转换到字符串t所需的最小编辑距离的方法。在这个问题中,允许的操作包括向左移动光标、向右移动、移到开头、移到结尾以及删除字符。给出每种操作的代价,任务是找到从s转换到t的最短路径。通过动态规划,可以找到最长的公共子序列,并计算所需的编辑步骤。对于无法通过这些操作实现的转换,答案为-1。该算法对于文本编辑和字符串比较等场景具有实际应用价值。

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;
    }
}

采用PyQt5框架与Python编程语言构建图书信息管理平台 本项目基于Python编程环境,结合PyQt5图形界面开发库,设计实现了一套完整的图书信息管理解决方案。该系统主要面向图书馆、书店等机构的日常运营需求,通过模块化设计实现了图书信息的标准化管理流程。 系统架构采用典型的三层设计模式,包含数据存储层、业务逻辑层和用户界面层。数据持久化方案支持SQLite轻量级数据库与MySQL企业级数据库的双重配置选项,通过统一的数据库操作接口实现数据存取隔离。在数据建模方面,设计了包含图书基本信息、读者档案、借阅记录等核心数据实体,各实体间通过主外键约束建立关联关系。 核心功能模块包含六大子系统: 1. 图书编目管理:支持国际标准书号、中国图书馆分类法等专业元数据的规范化著录,提供批量导入与单条录入两种数据采集方式 2. 库存动态监控:实时追踪在架数量、借出状态、预约队列等流通指标,设置库存预警阈值自动提醒补货 3. 读者服务管理:建立完整的读者信用评价体系,记录借阅历史与违规行为,实施差异化借阅权限管理 4. 流通业务处理:涵盖借书登记、归还处理、续借申请、逾期计算等标准业务流程,支持射频识别技术设备集成 5. 统计报表生成:按日/月/年周期自动生成流通统计、热门图书排行、读者活跃度等多维度分析图表 6. 系统维护配置:提供用户权限分级管理、数据备份恢复、操作日志审计等管理功能 在技术实现层面,界面设计遵循Material Design设计规范,采用QSS样式表实现视觉定制化。通过信号槽机制实现前后端数据双向绑定,运用多线程处理技术保障界面响应流畅度。数据验证机制包含前端格式校验与后端业务规则双重保障,关键操作均设有二次确认流程。 该系统适用于中小型图书管理场景,通过可扩展的插件架构支持功能模块的灵活组合。开发过程中特别注重代码的可维护性,采用面向对象编程范式实现高内聚低耦合的组件设计,为后续功能迭代奠定技术基础。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
"educational codeforces round 103 (rated for div. 2)"是一个Codeforces平台上的教育性比赛,专为2级选手设计评级。以下是有关该比赛的回答。 "educational codeforces round 103 (rated for div. 2)"是一场Codeforces平台上的教育性比赛。Codeforces是一个为程序员提供竞赛和评级的在线平台。这场比赛是专为2级选手设计的,这意味着它适合那些在算法和数据结构方面已经积累了一定经验的选手参与。 与其他Codeforces比赛一样,这场比赛将由多个问题组成,选手需要根据给定的问题描述和测试用例,编写程序来解决这些问题。比赛的时限通常有两到三个小时,选手需要在规定的时间内提交他们的解答。他们的程序将在Codeforces的在线评测系统上运行,并根据程序的正确性和效率进行评分。 该比赛被称为"educational",意味着比赛的目的是教育性的,而不是针对专业的竞争性。这种教育性比赛为选手提供了一个学习和提高他们编程技能的机会。即使选手没有在比赛中获得很高的排名,他们也可以从其他选手的解决方案中学习,并通过参与讨论获得更多的知识。 参加"educational codeforces round 103 (rated for div. 2)"对于2级选手来说是很有意义的。他们可以通过解决难度适中的问题来测试和巩固他们的算法和编程技巧。另外,这种比赛对于提高解决问题能力,锻炼思维和提高团队合作能力也是非常有帮助的。 总的来说,"educational codeforces round 103 (rated for div. 2)"是一场为2级选手设计的教育性比赛,旨在提高他们的编程技能和算法能力。参与这样的比赛可以为选手提供学习和进步的机会,同时也促进了编程社区的交流与合作。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值