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的物品,问是否存在取物方案使得价 ...
随机推荐
- Python元组详解
元组的特征 元组类型的名字是tuple 元组的一级元素不可被修改.不能增加或者删除: 元组和列表的书写区别是将中括号改成了小括号: 为方便区分元组和普通方法的参数,一般在元组的最后一个元素后保持加一个 ...
- LaTeX技巧008:如何给文字添加阴影效果?
大家可以使用这个包:shadowtext宏包
- 机器学习作业(八)异常检测与推荐系统——Matlab实现
题目下载[传送门] 第1题 简述:对于一组网络数据进行异常检测. 第1步:读取数据文件,使用高斯分布计算 μ 和 σ²: % The following command loads the datas ...
- lampp ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'lepus'
解决方法: 在[mysqlld]段下增加如下代码:skip-grant-tables: 1.which mysql 查看mysql位置,例如:/opt/lampp/bin/mysql 2.进入配置my ...
- js获取URL里的参数
第一种 通过正则获取URL中指定的参数 /** * 获取指定的URL参数值 * URL:http://www.xxx.com/index?name=123 * 参数:param URL参数 * 调用方 ...
- vmware运行ubuntu虚拟机出现诡异的鼠标闪烁
正在开心的写着AC自动机,突然发现鼠标消失了. 习惯性地动动鼠标,却还是没有反应,停止移动鼠标后鼠标却显现了出来??(吃惊.gif 在加载软件的时候,就算鼠标停止也会闪烁(其实这个虚拟机以前加载也会闪 ...
- react 渲染
目录 React渲染 createElement的三个参数 element如何生成真实节点 ReactDOMComponent 作用 ReactCompositeComponentWrapper 作用 ...
- jQuery---jQuery对象与DOM对象的区别
jQuery对象与DOM对象的区别 1. DOM对象:使用JavaScript中的方法获取页面中的元素返回的对象就是dom对象.2. jQuery对象:jquery对象就是使用jquery的方法获取页 ...
- 关于用js无法清除cookie
cookie名称相同时,未必是同一个. 因为Domain(站点)不同,路径不同. 用jquery.cookie清除cookie时,应当加上path属性: $.cookie("MedicalU ...
- SQL Server 检查和处理死锁问题
SELECT spid, blocked, DB_NAME(sp.dbid) AS DBName, program_name, waitresource, lastwaittype, sp.login ...