#598 (Div. 3) F.Equalizing Two Strings
Posted lwz_159
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了#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;
以上是关于#598 (Div. 3) F.Equalizing Two Strings的主要内容,如果未能解决你的问题,请参考以下文章
#598 (Div. 3) F.Equalizing Two Strings
Codeforces Round #598 (Div. 3)
Codeforces Round #598 (Div. 3) E. Yet Another Division Into Teams dp
Codeforces Round #598 (Div. 3) F Equalizing Two Strings(构造题)