题意

给定两个字符串,可以任意选择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. Linux扩展swap分区

    一.将sda磁盘进行分区: 先查看sda磁盘已经使用了多少主分区,如下图所示,主分区已使用3个,所以应选择扩展分区: 二.再将扩展分区进行分区: 三.分区完成后执行partprobe使系统重新识别分区 ...

  2. Vue组件使用

    一.组件概念 有html模板,有css样式,有js逻辑的集合体 每一个组件都是一个vue实例 每个组件均具有自身的模板template,根组件的模板就是挂载点 每个组件模板只能拥有一个根标签 子组件的 ...

  3. 翻译一篇英文文章,主要是给自己看的——在ASP.NET Core Web Api中如何刷新token

    原文地址 :https://www.blinkingcaret.com/2018/05/30/refresh-tokens-in-asp-net-core-web-api/ 先申明,本人英语太菜,每次 ...

  4. xpath元素定位方法

    XPath 使用路径表达式来选取 XML 文档中的节点或者节点集.这些路径表达式和我们在常规的电脑文件系统中看到的表达式非常相似.XPath 含有超过 100 个内建的函数.这些函数用于字符串值.数值 ...

  5. akka 的集群访问方式

    akka  中采用startProxy分区代理 访问 ,跟使用shardRegion 来访问的区别 这两种访问方式是不是重了呢. 而另外这是一个单例代理 private fun startUniver ...

  6. 读取文件信息,并通过sscanf从中获取所需数据

    #include <stdio.h> #include <stdlib.h> #include <string.h> int file_length(char* f ...

  7. leetcode-easy-others-190. Reverse Bits-NO

    mycode 不会... 参考: 1. 思路: 将十进制的n转换成二进制(str) -> 利用切片.反向获取不包含0b的反转后的二进制字符串 -> 补上0(共32位) 2. class S ...

  8. spring中常见注解描述

    @Qualifier如果一个接口类有多个实现类,那么可以用@Qualifier指定使用哪个实现类: /** * 定时器,用于处理超时的挂起请求,也用于连接断开时的重连. */ @Autowired @ ...

  9. Go(05)map介绍

    原文地址: http://www.limerence2017.com/2019/06/11/golang06/ 基本用法 map同样也是引用类型,map在使用前需要通过make进行初始化,否则会报pa ...

  10. JS获取表单元素的value

    <!-- 1.option selected属性,如果我们在下拉列表里面选择了一个option那么他的selected="true" ,如果我们想设置当前的option是选中 ...