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;
    }
    
  • 相关阅读:
    微信扫码登陆
    jquery的js代码兼容全部浏览器的解决方法
    如何才能成为一名优秀的架构师
    Bootstrap 完全教程笔记
    vue.js笔记总结
    dot.js模板实现分离式
    python基础知识1
    tensorflow基础知识1
    tensorflow基础知识
    常用python库文件
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11632924.html
Copyright © 2011-2022 走看看