首页 > 其他分享 >CF1701E Text Editor 题解报告

CF1701E Text Editor 题解报告

时间:2022-09-24 16:56:15浏览次数:49  
标签:le int 题解 CF1701E character text test 光标 Text

题意翻译

给定两个字符串 \(S, T\),初始时光标在串 \(T\) 尾部,你可以进行以下操作:

  • \(\texttt{left}\):将光标向左移动一个字符,如光标在字符串最左侧则不移动。
  • \(\texttt{right}\):将光标向右移动一个字符,如光标在字符串最右侧则不移动。
  • \(\texttt{home}\):将光标移动到字符串最左侧,如光标在字符串最左侧则不移动。
  • \(\texttt{end}\):将光标移动到字符串最右侧,如光标在字符串最右侧则不移动。
  • \(\texttt{backspace}\):将光标左侧的第一个字符从字符创中删除,如光标在字符串最左侧则不删除任何字符。

我们需要使 \(S\) 在做若干次操作后得到 \(T\),请你求出最小的操作次数。无解输出 \(-1\)。

题目描述

You wanted to write a text $ t $ consisting of $ m $ lowercase Latin letters. But instead, you have written a text $ s $ consisting of $ n $ lowercase Latin letters, and now you want to fix it by obtaining the text $ t $ from the text $ s $ . Initially, the cursor of your text editor is at the end of the text $ s $ (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 $ t $ from the text $ s $ using the given set of actions, or determine it is impossible to obtain the text $ t $ from the text $ s $ . You have to answer $ T $ independent test cases.

输入输出格式

输入格式

The first line of the input contains one integer $ T $ ( $ 1 \le T \le 5000 $ ) — the number of test cases. Then $ T $ test cases follow. The first line of the test case contains two integers $ n $ and $ m $ ( $ 1 \le m \le n \le 5000 $ ) — the length of $ s $ and the length of $ t $ , respectively. The second line of the test case contains the string $ s $ consisting of $ n $ lowercase Latin letters. The third line of the test case contains the string $ t $ consisting of $ m $ lowercase Latin letters. It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 5000 $ ( $ \sum n \le 5000 $ ).

输出格式

For each test case, print one integer — the minimum number of moves required to obtain the text $ t $ from the text $ s $ using the given set of actions, or -1 if it is impossible to obtain the text $ t $ from the text $ s $ 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

CODE

#include<bits/stdc++.h>
using namespace std;
const int N=5e3+2;
int n,m,l[N],r[N],f[N][N],T;
char s[N],t[N];
int main(){
	scanf("%d",&T);
	while(T--){ 
		scanf("%d%d",&n,&m);
		scanf("%s%s",s+1,t+1);
		int fg=0;
		for(int i=1,j=1;i<=m;i++,j++){
			while(s[j]!=t[i]&&j<=n) j++;
			if(j>n) {
				printf("-1\n");
				fg=1;
				break;
			}
			l[i]=j;//s与ti匹配的最前位置 
		}
		if(fg) continue;
		for(int i=m,j=n;i>=1;i--,j--){
			while(s[j]!=t[i]) j--;
			r[i]=j;//s与ti匹配的最后位置 
		}
		r[m+1]=0x3f3f3f3f;
		int ans=n;
		for(int i=1;i<=m;i++){
			for(int j=1;j<=n;j++){
				f[j][i]=0; 
				if(s[j]==t[i]){//一样滴
					f[j][i]=f[j-1][i-1]+1;//往左挪一个就

标签:le,int,题解,CF1701E,character,text,test,光标,Text
From: https://www.cnblogs.com/Aurora1217/p/16725947.html

相关文章

  • CF Round 822 Div2 题解
    比赛链接A题SelectThreeSticks(签到)给定\(n\)根木棒,第\(i\)根木棒的长度为\(a_i\)。现在我们可以进行操作,每次操作选定一根木棒,将其长度增高或减少1。问至少需......
  • CF 教育场 135 题解
    比赛链接A题ColoredBalls:Revisited(签到)给定\(n\)种颜色的球,其中颜色\(i\)的球的数量是\(cnt_i\),保证\(\sum\limits_{i=1}^ncnt_i\)是奇数。在一次操作中,我......
  • 2022 年你需要知道的关于 React Context 的一切,第 1 部分
    文章摘录2022年你需要知道的关于ReactContext的一切,第1部分摘录自快速反应,第二版作者:莫滕·巴克伦德这段摘录探讨了使用ReactContext。如果您是React开发......
  • 无法将类型string隐式转换为textbox wpf中
    大致意思是这样的,想把我获取到的字符串放入textbox中,然后我就写了这么一句a.textbox="获取到的字符串"  然后就有了下面的错误,啥也不说,强转a.textbox=(Textbox)"获取......
  • Codeforces Round #822 Div.2 整场题解
    目前还有E,F没有更新。A.SelectThreeSticks直接对\(a\)排序,选出来的木棍一定是相邻三项,都往中间靠更优。B.Bright,Nice,Brilliant最优方案就是每一行第一个......
  • 牛客题解 装进肚子
    链接:https://ac.nowcoder.com/acm/problem/14721来源:牛客网题解作者岛田小雅这道题刚拿到手,就感觉是个很简单(事实证明并不是很简单)的贪心。虽然我不是很会判断贪心的......
  • bug记录|NON-STATIC METHOD CANNOT BE REFERENCED FROM A STATIC CONTEXT
    bug记录|NON-STATICMETHODCANNOTBEREFERENCEDFROMASTATICCONTEXT  问题:原因:静态方法无法调用自己定义的非静态方法解决方案:1.改变非静态方法为静态方法,......
  • pycharm打字卡顿问题解决
    问题描述:我在pycharm中使用的远程服务器中的环境,工程也是本机映射到远程环境中,在某次断网以后,再次使用就变得非常卡,具体现象就是我码字要等,整个pycharm就无法点击,过了5秒以......
  • P1347 排序 题解
    题干交了8次,下载了3个测点.....首先这个题,很容易想到用拓扑如果有“X$<$Y”,就建立一条从X到Y的有向边要考虑到,如果排序成立,必须满足入度为0的点只有一个并且出度为0的......
  • P5089 [eJOI2018] 元素周期表 题解
    \(Preface\)主要是想刷点咕值然后就写了一写。。。顺便扔到博客园这边。题解题目传送门这道题嘛..主要还是找性质推规律。拿到题,第一眼:噢噢爆搜啊。第二眼:噢噢贪心啊......