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网络编程之TCP通讯
    java网络编程之UDP通讯
    Java中的线程同步机制
    阿里研发工程师面试题三个小结
    Android开发的进阶之路
    获取一个字符串中每一个字母出现的次数使用map集合
    Android常见面试题目
    Java垃圾回收
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11632924.html
Copyright © 2011-2022 走看看