当前位置:网站首页>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;
}
边栏推荐
- 简单又有效的基本折线图制作方法
- LeetCode_Digit Statistics_Medium_400. Nth Digit
- 《富爸爸,穷爸爸》思维导图和学习笔记
- leetcode 899. 有序队列
- Transformer怎么入门?如何学习Transformer?
- Use setTimeout to realize setInterval
- ECCV 2022 | 清华&腾讯AI Lab提出REALY:重新思考3D人脸重建的评估方法
- 剑指 Offer 07. 重建二叉树
- idea2021配置svn报错Cannot run program “svn“ (in directory “xxx“):CreateProcess error=2,系统找不到指定的文件
- XSS线上靶场---prompt
猜你喜欢
李沐动手学深度学习V2-BERT微调和代码实现
博士申请 | 美国明尼苏达大学葛畅教授招收隐私数据管理方向全奖博士/硕士/博后/访问学者...
从开发到软件测试:除了扎实的测试基础,还有哪些必须掌握 ?
详解虚拟机!京东大佬出品 HotSpot VM 源码剖析笔记(附完整源码)
From September 1st, my country has granted zero-tariff treatment to 98% of tax items from 16 countries including Togo
461. 汉明距离
CheckBox列表项选中动画js特效
StoneDB 开源社区月刊 | 202207期
肝完 Alibaba 这份面试通关宝典,我成功拿下今年第 15 个 Offer
图神经网络怎么入门?一文带你了解图神经网络入门路径-GNN入门
随机推荐
ES6解构赋值--数组解构及对象解构
svg+js订单确认按钮动画js特效
2022/08/03 学习笔记 (day23)多线程(补充)
XSS测试
开源一夏 |如何优化线上服务器
leetcode 125. 验证回文串
面试官:为什么 0.1 + 0.2 == 0.300000004?
abs()、fabs() 和 labs() 的区别
LeetCode_Digit Statistics_Medium_400. Nth Digit
Likou 59 - Spiral Matrix II - Boundary Judgment
跨端开发技术储备记录
leetcode 461. Hamming Distance
华为设备配置VRRP与BFD联动实现快速切换
leetcode 899. 有序队列
Zero trust, which has been popular for more than ten years, why can't it be implemented?
如何使用 Jmeter获取登录token并设置为全局变量?
Advantages and Disadvantages of Blind and Buried Via PCB Stacked Via Design
从开发到软件测试:除了扎实的测试基础,还有哪些必须掌握 ?
Leetcode 16. Numerical integral power (power + fast recursive/iteration)
leetcode 2119. 反转两次的数字