C. awoo‘s Favorite Problem(思维)

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

Problem - C - Codeforces

我们发现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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值