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;
    }
    
  • 相关阅读:
    赏月斋源码共享计划 第三期
    概率校准与Brier分数
    模型评估——定量分析预测的质量
    Sklearn实现逻辑回归
    赏月斋源码共享计划 第二期
    技术路线范文
    开题报告范文
    赏月斋源码共享计划 第一期
    关于研究课题中的技术路线与实施方案
    Git项目的初始化
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11632924.html
Copyright © 2011-2022 走看看