题目:给你三个字符串A,B,C(每个里只包含大写’ABC’,|A|=|B|=|C|,N)和一个数字P<=N
输出其中一个S 使得 match(A,S)>=P, match(B,S)<P, match(C,S)< P 没有就输出Impossible
当中match是相同位置相同字母的数量
例如
4 2
AABC
BBCC
ACAC
可以输出AABA
因为match(A,S)=3,match(B,S)=0,match(C,S)=1
6 6
AAAAAA
AAAAAA
BBBBBB
则输出 Impossible
我的思路
贪心,对每一个位置有四种可能
A[i]!=B[i], A[i]!=C[i]
A[i]==B[i], A[i]!=C[i]
A[i]==C[i], A[i]!=B[i]
A[i]==B[i]==C[i]
最后统计matchAS是多少
难道我思路有问题吗,感觉很正常,代码也想不到有什么问题
以下是代码
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
inline char other(char c1, char c2) {
if (c1 > c2) swap(c1, c2);
char ans = (c1 == 'A') ? (c2 == 'B' ? 'C' : 'B') : 'A';
return ans;
}
int n, p, aa, bb, cc; //aa bb cc 是 score
string a, b, c, s; // s是输出答案
char x, y, z;
int main() {
cin >> n >> p >> a >> b >> c;
for (int i = 0; i < n; i ++) {
x = a[i], y = b[i], z = c[i];
if (x != y && x != z) {
s.push_back(x);
aa ++;
}
if (x == y && x != z) {
if (bb < p - 1) {
s.push_back(x);
aa ++; bb ++;
} else {
s.push_back(other(y, z));
}
}
if (x == z && x != y) {
if (cc < p - 1) {
s.push_back(x);
aa ++; cc ++;
} else {
s.push_back(other(y, z));
}
}
if (x == y && x == z) {
if (bb < p - 1 && cc < p - 1) {
s.push_back(x);
aa ++; bb ++; cc ++;
} else {
s.push_back(other(y, z));
}
}
}
if (aa >= p) {
cout << s << endl;
} else {
cout << "Impossible" << endl;
}
}
谢谢