CF1215 C Swap Letters(思维题)

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

 

转载于:https://www.cnblogs.com/Yokel062/p/11615105.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值