zoukankan      html  css  js  c++  java
  • Codeforces Round #585 (Div. 2) C. Swap Letters

    链接:

    https://codeforces.com/contest/1215/problem/C

    题意:

    Monocarp has got two strings s and t having equal length. Both strings consist of lowercase Latin letters "a" and "b".

    Monocarp wants to make these two strings s and t equal to each other. He can do the following operation any number of times: choose an index pos1 in the string s, choose an index pos2 in the string t, and swap spos1 with tpos2.

    You have to determine the minimum number of operations Monocarp has to perform to make s and t equal, and print any optimal sequence of operations — or say that it is impossible to make these strings equal.

    思路:

    考虑三种情况, (a, b)(a, b), 这样一步就能交换成(a, a)(b, b), (b, a)(b, a)同理.
    (a, b)(b, a)这种情况先要交换成(a, a)(b, b)再交换, 多一步.
    记录(a, b)和(b, a)的数量, 总和需为偶数.
    再组内两两匹配, 如果多出来就多一步.

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    const int MAXN = 2e5+10;
    
    char s[MAXN], t[MAXN];
    vector<int > a, b;//a b, b a
    int n;
    
    int main()
    {
        cin >> n;
        cin >> s >> t;
        for (int i = 0;i < n;i++)
        {
            if (s[i] == t[i])
                continue;
            if (s[i] == 'a')
                a.push_back(i+1);
            if (s[i] == 'b')
                b.push_back(i+1);
        }
        if ((a.size()+b.size())%2)
        {
            puts("-1");
            return 0;
        }
        int cnt = (a.size()+b.size())/2;
        if (a.size()%2)
            cnt++;
        cout << cnt << endl;
        for (int i = 0;i+1 < a.size();i +=2)
            cout << a[i] << ' ' << a[i+1] << endl;
        for (int i = 0;i+1 < b.size();i += 2)
            cout << b[i] << ' ' << b[i+1] << endl;
        if (a.size()%2)
        {
            cout << *a.rbegin() << ' ' << *a.rbegin() << endl;
            cout << *a.rbegin() << ' ' << *b.rbegin() << endl;
        }
    
    
        return 0;
    }
    
  • 相关阅读:
    程序编译与代码优化 -- 早期(编译期)优化
    Java字节码指令
    知识点
    Openresty配置文件上传下载
    Openresty + nginx-upload-module支持文件上传
    G1日志分析
    Garbage First(G1)垃圾收集器
    Java内存分析工具jmap
    编译JDK1.7
    Java服务CPU占用高问题定位方法
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11632924.html
Copyright © 2011-2022 走看看