码迷,mamicode.com
首页 > 其他好文 > 详细

CF1215 C Swap Letters(思维题)

时间:2019-10-01 13:51:50      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:amp   ||   ini   ast   min   cat   using   for   preview   

链接: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.

 

题意:给出由a,b组成的2行等长度的字符串,问是否上下字符交换后相等,如果相等,输出交换次序,否则-1

题解:对于s[i], t[i],不相等的时候只有两个类型(a,b) 或者(b,a),然后对相同类型匹配,如果两个类型均剩余一个或者都不剩,即能相等,否则不能相等。

技术图片
#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

 

CF1215 C Swap Letters(思维题)

标签:amp   ||   ini   ast   min   cat   using   for   preview   

原文地址:https://www.cnblogs.com/Yokel062/p/11615105.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!