zoukankan      html  css  js  c++  java
  • 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
    ", ans.size());
        for(int i=0; i<ans.size(); i++)
            printf("%d %d
    ", ans[i].first, ans[i].second);
        return 0;
    }
    View Code
  • 相关阅读:
    atomic,nonatomic
    iOS开发-retain/assign/strong/weak/copy/mutablecopy/autorelease区别
    MagicalRecord的使用(第三方库实现的数据库)
    深浅拷贝
    C中的预编译宏定义
    省电的iPhone定位
    ASP连接读写ACCESS数据库实例(转)
    【摘要】JavaScript 的性能优化:加载和执行
    PC端网站跳转手机端网站
    多行未知文本垂直居中
  • 原文地址:https://www.cnblogs.com/Yokel062/p/11615105.html
Copyright © 2011-2022 走看看