Codeforces Round #585 (Div. 2) C. Swap Letters

前端之家收集整理的这篇文章主要介绍了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,a)同理.
(a,b)(b,a)这种情况先要交换成(a,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;
}

猜你在找的CSS相关文章