#598 (Div. 3) F.Equalizing Two Strings

题目描述

You are given two strings s and t both of length n and both consisting of lowercase Latin letters.
In one move, you can choose any length len from 1 to n and perform the following operation:
Choose any contiguous substring of the string s of length len and reverse it;
at the same time choose any contiguous substring of the string t of length len and reverse it as well.
Note that during one move you reverse exactly one substring of the string s and exactly one substring of the string t.
Also note that borders of substrings you reverse in s and in t can be different, the only restriction is that you reverse the substrings of equal length. For example, if len=3 and n=5, you can reverse s[1…3] and t[3…5], s[2…4] and t[2…4], but not s[1…3] and t[1…2].
Your task is to say if it is possible to make strings s and t equal after some (possibly, empty) sequence of moves.
You have to answer q independent test cases.

Input

The first line of the input contains one integer q (1≤q≤104) — the number of test cases. Then q test cases follow.
The first line of the test case contains one integer n (1≤n≤2⋅105) — the length of s and t.
The second line of the test case contains one string s consisting of n lowercase Latin letters.
The third line of the test case contains one string t consisting of n lowercase Latin letters.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 (∑n≤2⋅105).

Output

For each test case, print the answer on it — “YES” (without quotes) if it is possible to make strings s and t equal after some (possibly, empty) sequence of moves and “NO” otherwise.

Example

Input
4
4
abcd
abdc
5
ababa
baaba
4
asdf
asdg
4
abcd
badc
Output
NO
YES
NO
YES

题目大意

给你两个只包含小写字母的字符串s和t,你可以对它进行如下操作:在s和t中分别选择一个相同长度的子串,并反转它们(只需要保证两个子串长度相同即可)。问能否通过该操作使得s和t相等(操作次数不限)。

题目分析

为了最后能使s和t相等,所以首先要保证s和t中的字母都是相同的(条件1)当s和t中有某个字母的次数大于1时(且满足条件1),那么最后的结果肯定是YES(条件2)。因为只要其中一个一直交换两个相同的字符,另一个在慢慢调整即可。
如果满足条件1但不满足条件2,那么就根据字典序来统计交换的次数(类似冒泡排序),最后判断得到的两个交换次数的奇偶性即可(条件3)。当奇偶性相同时,即为YES;否则为NO。因为当奇偶性相同时,两者必然差了偶数次的交换,而一个字符串的相同位置不断的反转偶数次,这个字符串不会变。

代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <iomanip>
#define LL long long
#define PII pair<int,int>
using namespace std;
const int N=26;
int a[N],b[N]; 
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		memset(a,0,sizeof a);
		memset(b,0,sizeof b);
		int n;
		string s,t;
		cin>>n>>s>>t;
		for(int i=0;i<n;i++)
		{
			a[s[i]-'a']++;
			b[t[i]-'a']++;
		}
		bool flag=false,ok=true;
		for(int i=0;i<26;i++)
		{
			if(a[i]!=b[i]) ok=false;	//判断条件1
			if(a[i]>1) flag=true;		//判断条件2
		}
		if(!ok)
		{
			puts("NO");
			continue;
		}
		if(flag)
		{
			puts("YES");
			continue;
		}
		int is=0,it=0;			//判断条件3
		for(int i=0;i<n;i++)
		for(int j=i+1;j<n;j++)
		{
			is+=s[i]>s[j];
			it+=t[i]>t[j];
		}
		if((is&1)==(it&1)) puts("YES");
		else puts("NO");
	}
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lwz_159

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值