跳到正文

题解

P12817 删除编辑 题解

SunShine2012约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年6月15日
收录
2025年12月12日
更新
2026年11月5日
篇幅
约 2 分钟
获赞
2
题目
P12817

版本

  1. v1当前

题目分析

显而易见,ss 与 tt 完全相同时 YES,ss 比 tt 还短时 NO。

C++
if (s == t){cout << "YES\n"; continue;}
if (s.size() < t.size()){cout << "NO\n"; continue;}

对于其他情况,可以发现,

逆序找 tit_i 在 ss 的倒序中的位置情况:

C++
reverse(s.begin(), s.end());

不存在时 NO;

如果找到第一个后则删除第一个,那么每次找到的第一个 tit_i 升序排列,这是因为每次的 tit_i 在 ss 的倒序中都必须是第一个出现的 tit_i,不然后面就无法删除。

C++
//<bool in() 函数>
for (int i = t.size() - 1, _pos = 0; i >= 0; i--){
	int pos = s.find(t[i]);
	if (pos == string::npos) return 0;
	if (pos < _pos) return 0;
	s[_pos = pos] = ' ';
}return 1;

完整代码

C++
#include <bits/stdc++.h>
using namespace std;

#define int long long
int n;
string s, t;
bool in(){
	reverse(s.begin(), s.end());
	for (int i = t.size() - 1, _pos = 0; i >= 0; i--){
		int pos = s.find(t[i]);
		if (pos == string::npos) return 0;
		if (pos < _pos) return 0;
		s[_pos = pos] = ' ';
	}return 1;
}
signed main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n;
	while (n--){
		cin >> s >> t;
		if (s == t){cout << "YES\n"; continue;}
		if (s.size() < t.size()){cout << "NO\n"; continue;}
		if (in()) cout << "YES\n";
		else cout << "NO\n";
	}return 0;
}
原文