C. awoo‘s Favorite Problem--Educational Codeforces Round 130 (Rated for Div. 2)

69 篇文章 0 订阅
12 篇文章 1 订阅

Problem - 1697C - Codeforces

C. awoo's Favorite Problem

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given two strings ss and tt, both of length nn. Each character in both string is 'a', 'b' or 'c'.

In one move, you can perform one of the following actions:

  • choose an occurrence of "ab" in ss and replace it with "ba";
  • choose an occurrence of "bc" in ss and replace it with "cb".

You are allowed to perform an arbitrary amount of moves (possibly, zero). Can you change string ss to make it equal to string tt?

Input

The first line contains a single integer qq (1≤q≤1041≤q≤104) — the number of testcases.

The first line of each testcase contains a single integer nn (1≤n≤1051≤n≤105) — the length of strings ss and tt.

The second line contains string ss of length nn. Each character is 'a', 'b' or 'c'.

The third line contains string tt of length nn. Each character is 'a', 'b' or 'c'.

The sum of nn over all testcases doesn't exceed 105105.

Output

For each testcase, print "YES" if you can change string ss to make it equal to string tt by performing an arbitrary amount of moves (possibly, zero). Otherwise, print "NO".

Example

input

Copy

5
3
cab
cab
1
a
b
6
abbabc
bbaacb
10
bcaabababc
cbbababaac
2
ba
ab

output

Copy

YES
NO
YES
YES
NO

=========================================================================

ab ->ba

bc->cb

第一个置换只能有两个作用,那就是aaaab把b放到前面,abbbb把a放在后面

第二个置换同理

很自然的贪心。我们从末尾往前扫描,如果相等那么继续,否则,如果b对a,往前找a, c对b,往前找b,找不到或者不是这种配对,那么就无解。做多了感觉瞬间来。

# include <iostream>
# include<algorithm>
# include<cstring>
# include<vector>
# include<queue>
# define mod 1000000007

using namespace std;
typedef long long int ll;

int main()
{

   int t;
   cin>>t;

   while(t--)
   {
       int n;

       cin>>n;
       string s,t;

       cin>>s>>t;

       int flag=0;

       for(int i=n-1;i>=0;i--)
       {
           if(s[i]==t[i])
            continue;

           if(s[i]=='b'&&t[i]=='a')
           {

               int j;
               for( j=i-1;j>=0;j--)
               {
                   if(s[j]=='a'||s[j]=='b')
                   {
                       if(s[j]=='a')
                       {
                           swap(s[j],s[i]);

                           break;
                       }
                   }
                   else
                   {
                       flag=1;

                       break;

                   }
               }

               if(j==-1)
                flag=1;


           }
           else if(s[i]=='c'&&t[i]=='b')
           {
               int j;
               for( j=i-1;j>=0;j--)
               {
                   if(s[j]=='c'||s[j]=='b')
                   {
                       if(s[j]=='b')
                       {
                           swap(s[j],s[i]);

                           break;
                       }
                   }
                   else
                   {
                       flag=1;

                       break;
                   }
               }

               if(j==-1)
                flag=1;

           }
           else
           {
               flag=1;
               break;
           }
       }

       if(flag)
       {
           cout<<"NO"<<endl;

       }
       else
       {
           cout<<"YES"<<endl;
       }
   }


    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秦三码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值