当前位置:网站首页>C. awoo‘s Favorite Problem--Educational Codeforces Round 130 (Rated for Div. 2)
C. awoo‘s Favorite Problem--Educational Codeforces Round 130 (Rated for Div. 2)
2022-08-03 21:03:00 【秦小咩】
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;
}
边栏推荐
- 3种圆形按钮悬浮和点击事件
- 解决npm -v查看npm版本出现npm WARN config global `--global`, `--local` are deprecated. Use `--location报错
- Leetcode 125. Verify palindrome string
- ES6 - Arrow Functions
- 检测和控制影子IT的五个步骤
- leetcode 1837. K 进制表示下的各位数字总和
- 云图说丨初识华为云微服务引擎CSE
- 面试官:为什么 0.1 + 0.2 == 0.300000004?
- 系统运维系列 之CSV文件读取时内容中包含逗号的处理方法
- leetcode refers to Offer 58 - II. Left Rotate String
猜你喜欢

云图说丨初识华为云微服务引擎CSE

win10安装及配置Gradle

详解虚拟机!京东大佬出品 HotSpot VM 源码剖析笔记(附完整源码)

字节跳动软件测试岗,前两面过了,第三面HR天坑,结局透心凉...

DDD 中的几个困难问题

Lecture topics and guest blockbuster, TDengine developers conference to promote data technology "broken"

2022年1~7月语音合成(TTS)和语音识别(ASR)论文月报

伪标签汇总

【HiFlow】经常忘记签到怎么办?使用腾讯云场景连接器每天提醒你。

error: C1083: 无法打开包括文件: “QString”: No such error: ‘QDir‘ file not found
随机推荐
直播平台怎么搭建,针对输入框的各种组件
Transformer怎么入门?如何学习Transformer?
双线性插值公式推导及Matlab实现
idea2021配置svn报错Cannot run program “svn“ (in directory “xxx“):CreateProcess error=2,系统找不到指定的文件
开源一夏 |如何优化线上服务器
【HiFlow】经常忘记签到怎么办?使用腾讯云场景连接器每天提醒你。
【kali-漏洞扫描】(2.1)Nessus下载安装(上)
leetcode 268. 丢失的数字(异或!!)
【kali-漏洞利用】(3.2)Metasploit基础(上):基础知识
Advantages and Disadvantages of Blind and Buried Via PCB Stacked Via Design
leetcode refers to Offer 58 - II. Left Rotate String
dataframe 多层索引 更换索引 df.swaplevel(axis=1)
StoneDB 助力 2022 开放原子全球开源峰会
太香了! 阿里 Redis 速成笔记, 从头到尾全是精华!
模板字符串
canvas螺旋动画js特效
直播小程序源码,UI自动化中获取登录验证码
关于shell脚本的一些思考
在树莓派上搭建属于自己的网页(3)
leetcode 461. 汉明距离