zoukankan      html  css  js  c++  java
  • F. Equalizing Two Strings 题解(思维)

    题目链接

    题目思路

    对于这种区间反转问题,要把他想到是变为长度为2的区间反转,类比于冒泡排序

    因为所有的反转都能用冒泡排序的思想实现

    如果字符不同肯定不行

    然后如果某个字符出现次数大于1的话肯定可以,因为可以一直拿着他动,去改变其他人

    然后就是每个字符出现一次的情况,每次交换两个相邻的数就改变了逆序对的奇偶性

    而两个串同时改变奇偶性,所以要求两个串的逆序对数量相同

    代码

    #include<bits/stdc++.h>
    #define fi first
    #define se second
    #define debug cout<<"I AM HERE"<<endl;
    using namespace std;
    typedef long long ll;
    const int maxn=2e5+5,inf=0x3f3f3f3f,mod=1e9+7;
    const double eps=1e-6;
    int n;
    char s[3][maxn];
    int cnt[3][30];
    signed main(){;
        int _;scanf("%d",&_);
        while(_--){
            memset(cnt,0,sizeof(cnt));
            scanf("%d %s %s",&n,s[1]+1 ,s[2]+1);
            for(int i=1;i<=n;i++){
                cnt[1][s[1][i]-'a'+1]++;
                cnt[2][s[2][i]-'a'+1]++;
            }
            bool pr=1;
            bool flag=0;
            for(int i=1;i<=26;i++){
                if(cnt[1][i]!=cnt[2][i]){
                    pr=0;
                    break;
                }
                if(cnt[1][i]>=2){
                    flag=1;
                }
            }
            if(pr==0){
                printf("No
    ");
                continue;
            }
            if(pr&&flag){
                printf("Yes
    ");
                continue;
            }
            int sum[3]={0,0,0};
            for(int i=1;i<=n;i++){
                for(int j=i+1;j<=n;j++){
                    sum[1]+=(s[1][i]>s[1][j]);
                    sum[2]+=(s[2][i]>s[2][j]);
                }
            }
            if(sum[1]%2!=sum[2]%2){
                printf("No
    ");
            }else{
                printf("Yes
    ");
            }
        }
        return 0;
    }
    
    
    
    不摆烂了,写题
  • 相关阅读:
    chartjs 初步
    QT “error: error writing to -: Invalid argument”
    OTL mySQL
    qtcteater pro 文件配置
    Python os.readlink() 方法
    Python os.read() 方法
    Python os.popen() 方法
    Python os.pipe() 方法
    Python os.pathconf() 方法
    ThinkPHP框架前后台的分页调用
  • 原文地址:https://www.cnblogs.com/hunxuewangzi/p/15403994.html
Copyright © 2011-2022 走看看