题意

给定两个字符串,可以任意选择s串的一段和t串的相同长度的一段进行翻转,无限次数,问能否通过翻转使得两个字符串相等。

分析

  • 看了题解发现思路很巧妙。
  • 无限次数的子串翻转其实就是相邻两个字符的交换。
  • 首先字符串出现次数不同的肯定不行。
  • 然后如果某个字符出现次数大于1的,肯定可以,证明就是,先将s串通过交换相邻两个字符得到有序字符串,t串对应的可以随便交换;此时s串是有序的,且存在至少一对相邻字符是相同的,那么接下来t串通过交换相邻的字符得到有序字符串,同时s串就只交换一对相同的字符,保证s串不变。
  • 除去上面两个情况,剩下的就是字符最多只出现一次的,离散化就相当于一个排列,对于一个排列来说,交换两个相邻的数可以改变逆序数的奇偶性,两个串同时改变,也就是两个串的奇偶性保持一致,所以求出每个原串的逆序数判断即可。

代码

#include <bits/stdc++.h>
using namespace std;
const int N=2e5+50;
char s[N],t[N];
int q,n,cnt[30];
int c[120];
int lowbit(int x){
return x&(-x);
}
void add(int i,int x){
while(i<=100){
c[i]+=x;
i+=lowbit(i);
}
}
int sum(int i){
int ans=0;
while(i){
ans+=c[i];
i-=lowbit(i);
}
return ans;
}
int main(){
scanf("%d",&q);
while(q--){
scanf("%d",&n);
scanf("%s",s+1);
scanf("%s",t+1);
memset(cnt,0,sizeof(cnt));
bool dou=false;
for(int i=1;i<=n;i++){
cnt[s[i]-'a']++;
if(cnt[s[i]-'a']>=2){
dou=true;
}
}
for(int i=1;i<=n;i++){
cnt[t[i]-'a']--;
}
bool flag=true;
for(int i=0;i<26;i++){
if(cnt[i]){
flag=false;
break;
}
}
if(!flag){
printf("NO\n");
}else{
if(dou){
printf("YES\n");
}else{
memset(c,0,sizeof(c));
int ss=0;
for(int i=1;i<=n;i++){
ss+=i-1-sum(s[i]-'a'+1);
add(s[i]-'a'+1,1);
}
memset(c,0,sizeof(c));
int tt=0;
for(int i=1;i<=n;i++){
tt+=i-1-sum(t[i]-'a'+1);
add(t[i]-'a'+1,1);
}
if(ss%2==tt%2){
printf("YES\n");
}else{
printf("NO\n");
}
}
}
}
return 0;
}

Codeforces1256F_Equalizing Two Strings的更多相关文章

  1. Hacker Rank: Two Strings - thinking in C# 15+ ways

    March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...

  2. StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?

    StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...

  3. Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  4. [LeetCode] Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  5. [LeetCode] Encode and Decode Strings 加码解码字符串

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

  6. [LeetCode] Group Shifted Strings 群组偏移字符串

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  7. [LeetCode] Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  8. [LeetCode] Multiply Strings 字符串相乘

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  9. 使用strings查看二进制文件中的字符串

    使用strings查看二进制文件中的字符串 今天介绍的这个小工具叫做strings,它实现功能很简单,就是找出文件内容中的可打印字符串.所谓可打印字符串的涵义是,它的组成部分都是可打印字符,并且以nu ...

随机推荐

  1. 【CF963C】Cutting Rectangle(数论,构造,map)

    题意: 思路:考虑构造最小的单位矩形然后平铺 单位矩形中每种矩形的数量可以根据比例算出来,为c[i]/d,其中d是所有c[i]的gcd,如果能构造成功答案即为d的因子个数 考虑如果要将两种矩形放在同一 ...

  2. Mybatis 结果映射下划线转驼峰

    mybatis 结果映射下划线转驼峰 Spring Boot 配置: #下划线转驼峰 mybatis.configuration.map-underscore-to-camel-case=true m ...

  3. 01Two Sum题解

    Tow Sum 原题概述: Given an array of integers, return indices of the two numbers such that they add up to ...

  4. Link Script 学习

    最后更新 2019-06-27 概述 当使用 C 或者 C++ 编写代码实现某种功能时,需要将源代码进行编译以及链接.链接是将一系列目标文件(.o)以及归档文件(.a)组合起来,重新定位各个文件数据并 ...

  5. python学习之路(9)

    函数的参数 定义函数的时候,我们把参数的名字和位置确定下来,函数的接口定义就完成了.对于函数的调用者来说,只需要知道如何传递正确的参数,以及函数将返回什么样的值就够了,函数内部的复杂逻辑被封装起来,调 ...

  6. EDA cheat sheet

    %config InlineBackend.figure_format = 'svg' 在jupyter notebook中使用这个命令绘制更清晰的图像,注意百分号后不能有空格. 1. Univari ...

  7. 作业要求20191010-3 alpha week 1/2 Scrum立会报告+燃尽图 01

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2019fall/homework/8746 一.小组情况组长:贺敬文组员:彭思雨 王志文 位军营 杨萍队名:胜 ...

  8. c++匿名函数精简写法

    main.cpp: #include<stdio.h> #include<functional> #include<unistd.h> std::function& ...

  9. UVa 699 The Falling Leaves(递归建树)

    UVa 699 The Falling Leaves(递归建树) 假设一棵二叉树也会落叶  而且叶子只会垂直下落   每个节点保存的值为那个节点上的叶子数   求所有叶子全部下落后   地面从左到右每 ...

  10. electron之API学习

    学习一个新框架或者技术,最深入最全面的方法就是通过官方API,例如我们学习electron: 例如我们需要学习electron的BrowserWindow对象的使用,以及在创建她时我们可以配置的参数: ...