CF Harbour.Space Scholarship Contest 2021-2022 D
[link](Problem - D - Codeforces)
题意
给定一个字符串,从前往后一个一个枚举,对于每一个字母可以进行两种操作选取这个和不选这个并删除前一个(如果前面没有也可以成立),问是否可以通过合法的操作将给定的字符串变成提问的字符串。
题解
对于原串str中选取的字母之间必须相差为0或2的倍数,从前往后考虑,当选完要选的最后一个字母后剩下的不知道是否成立,因此从后往前考虑较好,从后往前每一个如果对就要,否则就跳2,因此如果存在合理的方案,那么最后一个一定是偶数的跳过去,或者等效替代前面的,一定要选。
例如:
axxbxxcxc 偶数跳
axxbxcxxc 等效替代
*Code
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <unordered_map>
#include <cmath>
#include <stack>
#include <iomanip>
#define x first
#define y second
#define eps 1e-7
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef unsigned long long ULL;
const int N = 1e5 + 10, M = 30010, INF = 0x3f3f3f3f, P = 131, mod = 1e9 + 7;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int n, m;
int a[N], s1[N], s2[N];
int main () {
// cin.tie(nullptr)->sync_with_stdio(false);
int T;
cin >> T;
while (T --) {
string str, t;
cin >> str >> t;
reverse(str.begin(), str.end()), reverse(t.begin(), t.end());
int n = str.size(), m = t.size();
bool ok = false;
for (int i = 0, j = 0; i < n; i ++ ) {
while (str[i] == t[j] && i < n) {
i ++ , j ++;
}
i ++;
if (j == m) {
ok = true;
break;
}
}
cout << (ok ? "YES" : "NO") << endl;
}
return 0;
}
本文解析了CF Harbour空间学者赛2021-2022 D题,通过从后向前遍历字符串的方式,讨论了如何通过特定的操作判断原字符串能否转换为目标字符串。该方法涉及字符串处理及算法逻辑。
874

被折叠的 条评论
为什么被折叠?



