贸易网站源码,微信分销系统软件开发,制作小程序代码,c网站开发教程传送门 文章目录题意#xff1a;思路#xff1a;题意#xff1a;
给你两个长度为nnn的串a,ba,ba,b#xff0c;每次可以同时翻转a,ba,ba,b中任意一段长度为L(1≤L≤n)L(1\le L\le n)L(1≤L≤n)的子串#xff0c;问能否通过若干次操作使两个串相同。
思路#xff1a;
首…传送门
文章目录题意思路题意
给你两个长度为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,tunenative)
//#pragma GCC optimize(2)
#includecstdio
#includeiostream
#includestring
#includecstring
#includemap
#includecmath
#includecctype
#includevector
#includeset
#includequeue
#includealgorithm
#includesstream
#includectime
#includecstdlib
#includerandom
#includecassert
#define X first
#define Y second
#define L (u1)
#define R (u1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].ltr[u].r)1)
#define Len(u) (tr[u].r-tr[u].l1)
#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 pairint,int PII;const int N1000010,mod1e97,INF0x3f3f3f3f;
const double eps1e-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 flagfalse;for(int i1;in;i) {c1[a[i]-a]; c2[b[i]-a];}for(int i0;i26;i) {if(c1[i]!c2[i]) return false;if(c1[i]1||c2[i]1) flag1;}if(flag) return true;LL sum1,sum2; sum1sum20;memset(c1,0,sizeof(c1));memset(c2,0,sizeof(c2));for(int i1;in;i) {c1[a[i]-a]; c2[b[i]-a];for(int ja[i]-a1;j26;j) sum1c1[j];for(int jb[i]-a1;j26;j) sum2c2[j];}if(sum1-sum21) return false;return true;
}int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);int _; scanf(%d,_);while(_--) {scanf(%d%s%s,n,a1,b1);puts(check()? YES:NO);}return 0;
}
/**/