CF1215 C Swap Letters (thinking title)

Links: https://codeforces.com/problemset/problem/1215/C

 
            Swap Letters (2 seconds)

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

Monocarp wants to make these two strings ss and tt equal to each other. He can do the following operation any number of times: choose an index pos1pos1 in the string ss, choose an index pos2pos2 in the string tt, and swap spos1spos1 with tpos2tpos2.

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

 

Input

The first line contains one integer n(1n2105)(1≤n≤2⋅105) — the length of ss and tt.

The second line contains one string ss consisting of nn characters "a" and "b".

The third line contains one string tt consisting of nn characters "a" and "b".

 
Output

If it is impossible to make these strings equal, print 1−1.

Otherwise, in the first line print kk — the minimum number of operations required to make the strings equal. In each of the next kk lines print two integers — the index in the string ss and the index in the string tt that should be used in the corresponding swap operation.

 

The meaning of problems: the length of the string given by the other two lines a, b of the composition, and asked whether the character is equal to the vertical exchange, if they are equal, changing the order of output, otherwise -1

Solution: for s [i], t [i], are not equal when only two types (a, b), or (b, a), then the same type of matching, if the remaining one or two types have not left that can equal or not equal.

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> Pii;

const int maxn=2e5+5;
char s[maxn], t[maxn];
vector<int> v1, v2;
vector<Pii> ans;

int main()
{
    int len;
    scanf("%d", &len);
    scanf("%s%s", s+1, t+1);
    for(int i=1; i<=len; i++)
    {
        if(s[i]=='a' && t[i]=='b') v1.push_back(i);
        if(s[i]=='b' && t[i]=='a') v2.push_back(i);
    }
    int lasta=0, lastb=0;
    for(int i=1; i<v1.size(); i=i+2)
        ans.push_back(Pii(v1[i], v1[i-1]));
    for(int i=1; i<v2.size(); i=i+2)
        ans.push_back(Pii(v2[i], v2[i-1]));
    if(v1.size()%2) lasta=v1[v1.size()-1];
    if(v2.size()%2) lastb=v2[v2.size()-1];

    if((lasta&&lastb==0) || (lasta==0&&lastb)){
        printf("-1"); return 0;
    }
    if(lasta){
        ans.push_back(Pii(lasta, lasta));
        ans.push_back(Pii(lasta, lastb));
    }
    printf("%d\n", ans.size());
    for(int i=0; i<ans.size(); i++)
        printf("%d %d\n", ans[i].first, ans[i].second);
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/Yokel062/p/11615105.html