我们发现a只能往右移,c只能往左移,且ac的不能互相换位置。b作为移动媒介其实是不影响答案的。
那么我们可以把b去掉,看看剩下的二者是否相同;
还有就是,相对于t串,s的a不能在后面,c不能在前面,不然也换不回来。
//jiangly的代码太优美了qaq
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n;
std::cin >> n;
std::string s, t;
std::cin >> s >> t;
for (auto c : { 'a', 'b', 'c' }) {
if (std::count(s.begin(), s.end(), c) != std::count(t.begin(), t.end(), c)) {
std::cout << "NO\n";
return;
}
}
std::string s1, t1;
std::vector<int> a, b;
for (int i = 0; i < n; i++) {
if (s[i] != 'b') {
s1 += s[i];
a.push_back(i);
}
}
for (int i = 0; i < n; i++) {
if (t[i] != 'b') {
t1 += t[i];
b.push_back(i);
}
}
if (s1 != t1) {
std::cout << "NO\n";
return;
}
for (int i = 0; i < int(a.size()); i++) {
if (s1[i] == 'a' ? (a[i] > b[i]) : (a[i] < b[i])) {
std::cout << "NO\n";
return;
}
}
std::cout << "YES\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}

该博客主要讨论了一个字符串处理的问题,其中涉及到字符计数、字符串比较和移动操作。作者通过简化问题,去除了不影响答案的字符'b',并检查'a'和'c'的位置是否允许它们在给定限制下互相移动。代码实现中,首先检查'a'和'c'的数量是否相等,然后对比不含'b'的子串,并验证'a'是否总是在'c'之前,以此判断能否将字符串s转换为t。这段代码展示了算法和字符串处理的优雅解决方案。
245

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



