题目大意:有两个由'R'和‘B’组成的字符串s1和s2,可以将s1或s2最右端的一个字符放到另一个字符串最右端,问能否使两个字符串中都不存在相邻两字母相等的情况
1<=n<=4e4
思路:因为只能移动最右端的字符,那么我们不妨直接考虑极端情况:将s2中最右端的字符依次移到s1最右端,也就是将s2首尾倒置以后连到s1最右端,然后我们要讨论的问题就是在哪里令这个新的字符串分成原来的两个字符串使其满足条件,因为能断开的位置只有一个,所以这个融合成的字符串不能有超过1个地方有两个相邻的相同字母
//#include<__msvc_all_public_headers.hpp>
#include<bits/stdc++.h>
using namespace std;
int main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--)
{
int n, m;
cin >> n >> m;
string s1, s2;
cin >> s1 >> s2;
reverse(s2.begin(), s2.end());//将s2首尾倒置
s1 += s2;//将s2连接到s1最右端
int flag = 0;
for (int i = 0; i <= n + m - 1; i++)
{
if (s1[i] == s1[i + 1])
{
if (flag)
{//第二次遇到,直接判为不合法
cout << "NO" << endl;
flag = 2;
break;
}
flag = 1;//记录出现相邻相同字母的次数
}
}
if (flag != 2)
{
cout << "YES" << endl;
}
}
return 0;
}