题意

给定两个字符串,可以任意选择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. 2002: [Hnoi2010]Bounce 弹飞绵羊(分块)

    2002: [Hnoi2010]Bounce 弹飞绵羊 时间限制: 10 Sec  内存限制: 259 MB 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他 ...

  2. JS框架_(JQuery.js)夜晚天空满天星星闪烁动画

    百度云盘 传送门 密码:xftr 满天星星闪烁动画效果: (可用星空动画来作为页面背景,白色文字改为文章或者其他的O(∩_∩)O) <!doctype html> <html> ...

  3. iOS13即将到来,iOS推送Device Token适配方案详解

    关于提前适配iOS13 苹果推送DeviceToken的通知 随着苹果iOS13系统即将发布,个推提前推出DeviceToken适配方案,以确保新版本的兼容与APP推送服务的正常使用.iOS13的一个 ...

  4. 分布式-信息方式-JMS可靠性机制

                        JMS的可靠性机制1.消息接收确认        JMS消息只有在被确认之后,才认为已经被成功地消费了.消息的成功消费通常包含三个阶段:客户接收消息.客户处理消 ...

  5. ssm框架文件配置

    1 简介 Spring MVC (web level),采取 MVC 架构,意图取代麻烦的 Servlet 写法,简化 web 层 MyBatis (dao level),意图取代 jdbc 操作数据 ...

  6. SNMP命令

    snmp命令 配置管理网络协议Weblogic项目管理Cisco Snmputil 命令 Snmputil是一个命令行下的软件,使用语法如下: usage: snmputil get|getnext| ...

  7. webdriver驱动火狐浏览器报错:Unable to find a matching set of capabilities

    raise exception_class(message, screen, stacktrace)selenium.common.exceptions.SessionNotCreatedExcept ...

  8. 第六周总结 & 实验报告(四)

    第六周小结 一.instanceof关键字         在Java中使用instanceof关键字判断一个对象到底是哪个类的实例,返回boolean类型 1.instanceof关键字的作用 例c ...

  9. 第八周学习总结&实验报告六

    实验总结 :类的继承 实验目的 理解异常的基本概念: 掌握异常处理方法及熟悉常见异常的捕获方法. 实验要求 : 练习捕获异常.声明异常.抛出异常的方法.熟悉try和catch子句的使用. 掌握自定义异 ...

  10. ubuntu16.04修改host上外網

    1.打开hosts文件: sudo emacs /etc/hosts 2.加入下面的内容 #chrome同步服务器 203.208.46.132 chrome.google.com203.208.46 ...