CF1215 C Swap Letters(思维题)
Posted yokel062
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF1215 C Swap Letters(思维题)相关的知识,希望对你有一定的参考价值。
链接:https://codeforces.com/problemset/problem/1215/C
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.
The first line contains one integer nn (1≤n≤2⋅105)(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".
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;
以上是关于CF1215 C Swap Letters(思维题)的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #619 (Div. 2)/CF1301 思维+贪心+模拟+构造+二维ST表
CF 750C New Year and Rating(思维题)