Codeforces Round #598 (Div. 3) F. Equalizing Two Strings
You are given two strings ss and tt both of length nn and both consisting of lowercase Latin letters.
In one move, you can choose any length lenlen from 11 to nn and perform the following operation:
- Choose any contiguous substring of the string ss of length lenlen and reverse it;
- at the same time choose any contiguous substring of the string tt of length lenlen and reverse it as well.
Note that during one move you reverse exactly one substring of the string ss and exactly one substring of the string tt.
Also note that borders of substrings you reverse in ss and in tt can be different, the only restriction is that you reverse the substrings of equal length. For example, if len=3len=3 and n=5n=5, you can reverse s[1…3]s[1…3] and t[3…5]t[3…5], s[2…4]s[2…4] and t[2…4]t[2…4], but not s[1…3]s[1…3] and t[1…2]t[1…2].
Your task is to say if it is possible to make strings ss and tt equal after some (possibly, empty) sequence of moves.
You have to answer qq independent test cases.
The first line of the input contains one integer qq (1≤q≤1041≤q≤104) — the number of test cases. Then qq test cases follow.
The first line of the test case contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of ss and tt.
The second line of the test case contains one string ss consisting of nn lowercase Latin letters.
The third line of the test case contains one string tt consisting of nn lowercase Latin letters.
It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105 (∑n≤2⋅105∑n≤2⋅105).
For each test case, print the answer on it — "YES" (without quotes) if it is possible to make strings ss and tt equal after some (possibly, empty) sequence of moves and "NO" otherwise.
4
4
abcd
abdc
5
ababa
baaba
4
asdf
asdg
4
abcd
badc
NO
YES
NO
YES
大概意思
现在给你两个字符串,你可以进行若干次操作。
每次操作需要在每个字符串都选择出长度为len的一个区间,两个区间位置不一定相同,然后将这个区间的字符都进行翻转。
问你进行若干次操作后,这俩字符串能变成一样的吗?
#include<bits/stdc++.h>
using namespace std;
int n;
string s1,s2,S1,S2;
//前两个满足, 如果两个字符串的逆的奇偶性相同,那么一定是YES
//在判断1和2之后,我们得到的一定是一个排列,问题就变成你可以翻转若干次,两个排列能否相同。
//我们考虑我们同时翻转相同长度的,我们排列的逆一定会发生奇偶性的变化,
//那么如果一开始奇偶性就不同,那么不管怎么翻转,都不会相同。
int Count(string s) {
int num=;
for(int i=; i<s.size(); i++) {
for(int j=; j<i; j++) {
if(s[j]>s[i])
num++;
}
}
return num;
}
void solve() {
cin>>n>>S1>>S2;
s1=S1;
s2=S2;
sort(s1.begin(),s1.end());
sort(s2.begin(),s2.end());
for(int i=; i<n; i++) {
if(s1[i]!=s2[i]) { //如果存在不同字符,那么一定不行
puts("NO");
return;
}
}
for(int i=; i<n; i++) {
if(s1[i]==s1[i-]) {//如果存在相同字符
puts("YES");
return;
}
if(s2[i]==s2[i-]) {
puts("YES");
return;
}
}
if(Count(S1)%==Count(S2)%) {
puts("YES");
} else {
puts("NO");
}
return;
}
int main() {
int t;
scanf("%d",&t);
while(t--)solve();
}
Codeforces Round #598 (Div. 3) F. Equalizing Two Strings的更多相关文章
- Codeforces Round #598 (Div. 3) F. Equalizing Two Strings 构造
F. Equalizing Two Strings You are given two strings s and t both of length n and both consisting of ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #598 (Div. 3)- E. Yet Another Division Into Teams - 动态规划
Codeforces Round #598 (Div. 3)- E. Yet Another Division Into Teams - 动态规划 [Problem Description] 给你\( ...
- 水题 Codeforces Round #302 (Div. 2) A Set of Strings
题目传送门 /* 题意:一个字符串分割成k段,每段开头字母不相同 水题:记录每个字母出现的次数,每一次分割把首字母的次数降为0,最后一段直接全部输出 */ #include <cstdio> ...
- Codeforces Round #598 (Div. 3)
传送门 A. Payment Without Change 签到. Code /* * Author: heyuhhh * Created Time: 2019/11/4 21:19:19 */ #i ...
- 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】
https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...
随机推荐
- cssflex兼容(横向顶边对齐)
display: flex; display: -webkit-box; display: -moz-box; display: -ms-flexbox; display: -webkit-flex; ...
- 《深入理解java虚拟机》读书笔记六——第七章
第七章 虚拟机类加载机制 1.类加载的时机 虚拟机的类加载机制: 虚拟机把描述类的数据从class文件中加载到内存,并对数据进行校验.转换解析和初始化,最终形成了可以被虚拟机直接使用的Java类型,这 ...
- ASP.NET Razor 常用示例
1.在网页中显示@符号 使用@@即可使编译器不切换到c#,这样在网页中会显示一个@符号. 2.隐式表达式 也就是正常的razor语法,不能包含空格.(除了await 如:<p>@await ...
- ubantu中的apache中设置代理
1.启用代理模块 a2enmod proxy proxy_balancer proxy_http 2.修改 /sites-available/000-default.conf 添加 <Virtu ...
- 多项式对数函数 - NTT
#include <bits/stdc++.h> using namespace std; #define int long long const int N=4000005; // 4 ...
- 使用Vmware过程中,突然网络连接不上问题
###第一次的解决方法: 1.我一般过一段时间就会对虚拟机进行拍快照备份:在使用过程中,如果没有太大变化,恢复网络正常的快照一般是能解决问题的,但是要记得恢复快照之前要备份你已经修改过的所有东西,以防 ...
- HTML之<meta>标签全解
一.定义 元素可提供相关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词等等. 标签位于文档的头部<head></head>标签内 ...
- QT版本
最近在linux下安装qt:发现主要的问题是qt的版本问题:下面来谈谈各个版本的理解 Qt 的版本是按照不同的图形系统来划分的,目前分为五个版本: Win: 适用于Miccrosoft Windows ...
- EasyExcel实现导入excel
https://blog.csdn.net/rexueqingchun/article/details/91870372 1.pom.xml配置依赖包 <!-- xls格式excel依赖包 -- ...
- Java实现图形界面的三部曲及IDE中的窗口设计
设计和实现图形用户界面的工作主要有以下几点: • (1)创建组件(Component) • 创建组成界面的各种元素,如按钮.文本框等.• (2)指定布局(Layout) • 根据具体需要排列它们的位置 ...