传送门
文章目录
- 题意:
- 思路:
题意:
给你两个长度为nnn的串a,ba,ba,b,每次可以同时翻转a,ba,ba,b中任意一段长度为L(1≤L≤n)L(1\le L\le n)L(1≤L≤n)的子串,问能否通过若干次操作使两个串相同。
思路:
首先他们包含的字符个数不同的话肯定是不能转换成相同的串的。
否则的话,通过观察,有个显然的结论:如果某个串有两个相同的字符,那么可以证明两个串一定可以变成一样的。
这个结论比较显然,我们先通过交换使两个相同字符相邻,让后再每次交换长度为222的字串的时候选择这个相邻的字符,这样这个串是不变的,而上面哪个串一定可以通过交换相邻位置的操作变成下面哪个串,所以是正确的。
那么当串中没有相同的字母的时候怎么办呢?我们还是考虑交换相邻两项,交换相邻两项?可以联想到逆序对,那么我们求出来两个串的逆序对个数,也就是将两个串排序之后的操作次数,如果两个串逆序对个数同奇偶,那么一定可以变成一样的。这个也比较显然,当某个串排序完成之后,可以交换相邻两项偶数次,这样相当于没有变化,一直到另一个串也排好序为止。
// Problem: F. Equalizing Two Strings
// Contest: Codeforces - Codeforces Round #598 (Div. 3)
// URL: https://codeforces.com/contest/1256/problem/F
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n;
char a[N],b[N];
int c1[26],c2[26];bool check() {memset(c1,0,sizeof(c1));memset(c2,0,sizeof(c2));bool flag=false;for(int i=1;i<=n;i++) {c1[a[i]-'a']++; c2[b[i]-'a']++;}for(int i=0;i<26;i++) {if(c1[i]!=c2[i]) return false;if(c1[i]>1||c2[i]>1) flag=1;}if(flag) return true;LL sum1,sum2; sum1=sum2=0;memset(c1,0,sizeof(c1));memset(c2,0,sizeof(c2));for(int i=1;i<=n;i++) {c1[a[i]-'a']++; c2[b[i]-'a']++;for(int j=a[i]-'a'+1;j<26;j++) sum1+=c1[j];for(int j=b[i]-'a'+1;j<26;j++) sum2+=c2[j];}if(sum1-sum2&1) return false;return true;
}int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);int _; scanf("%d",&_);while(_--) {scanf("%d%s%s",&n,a+1,b+1);puts(check()? "YES":"NO");}return 0;
}
/**/