贪心模拟 265个测试点里只有3WA
  • 板块学术版
  • 楼主garethhkm2023
  • 当前回复1
  • 已保存回复1
  • 发布时间2020/7/28 18:34
  • 上次更新2023/11/6 21:56:43
查看原帖
贪心模拟 265个测试点里只有3WA
185760
garethhkm2023楼主2020/7/28 18:34

题目:给你三个字符串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]

    • S += A[i] 不会对matchBS,matchCS有影响
  • A[i]==B[i], A[i]!=C[i]

    • 如果目前为止matchBS<p-1,则S+=A[i],matchBS++
    • 否则S+=X 而X是不出现在A[i],B[i],C[i]的字母
  • A[i]==C[i], A[i]!=B[i]

    • 同理
  • A[i]==B[i]==C[i]

    • 如果目前为止matchBS<p-1和matchCS<p-1,则S+=A[i],matchBS++,matchCS++
    • 否则S+=X 而X是不出现在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;
    }
}

谢谢

2020/7/28 18:34
加载中...