CodeForces - 1697C

题目链接: Problem - 1697C - Codeforces

ab->ba

bc->cb

这题的本质是a可以右移, c可以左移, b只是一个移动的工具, 不用管它(无论b怎么移动a和c的相对关系都不会被改变), 首先要做的第一步就是把b全部去掉对比a和c的相对性关系, 因为相对关系不正确无论如何移动都是错误的, 然后按照a可以右移, c可以左移的性质判断对错就好了

ps: 我的代码好像可以优化成O(n), 但我懒(

ac代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <stack>
#include <deque>
#include <map>
#include <set>
using namespace std;
#define ll long long
#define endl "\n"
#define rep(i, a, b) for (ll i = (a); i <= (b); i++)
#define repr(i, a, b) for (ll i = (a); i < (b); i++)
#define rrep(i, a, b) for (ll i = (b); i >= (a); i--)
#define rrepr(i, a, b) for (ll i = (b); i > (a); i--)
#define min(a,b) (a)<(b)?(a):(b)
#define max(a,b) (a)>(b)?(a):(b)

ll cnt,n,m,t,ans,ant;
const int N=1e5+10;
ll arr[N];
string str1;
string str2;
string str3;
string str4;

inline ll read()
{
    char c = getchar();int x = 0,s = 1;
    while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}//是符号
    while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}//是数字
    return x*s;
}


void solve()
{
    n=read();
    cin>>str1;
    cin>>str2;
    ant=cnt=0;
    if(str1==str2)
    {
        puts("YES");
        return;
    }
    str3.clear();
    str4.clear();
    repr(i,0,n)//相对位置不变
    {
        if(str1[i]!='b') str3.push_back(str1[i]);
        if(str2[i]!='b') str4.push_back(str2[i]);
    }
    if(str3!=str4)//相对位置不同必定不可能
    {
        puts("NO");
        return;
    }
    ll s_a,s_c,t_a,t_c;
    s_a=s_c=t_a=t_c=0;
    repr(i,0,n)//a只能右移 c只能左移
    {
        switch (str1[i])
        {
        case 'a': s_a++;break;
        case 'c': s_c++;
        }

        switch (str2[i])
        {
        case 'a': t_a++;break;
        case 'c': t_c++;
        }
        if(s_a<t_a)//因为a只能右移 所以s中a一定更早出现
        {
            puts("NO");
            return;
        }
        if(s_c>t_c)//c只能左移 s中的c可以更晚出现
        {
            puts("NO");
            return;
        }
    }
    puts("YES");
    // cout<<str3<<endl;
    // cout<<str4<<endl;
    return;
}

int main()
{
    t=read();
    while(t--)
        solve();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值