페이지

2498번: 중앙문자열

https://www.acmicpc.net/problem/2498


#include<cstdio>
#include<algorithm>
using namespace std;
const int MAX_N = 1e5;
char str[3][MAX_N + 1], r[MAX_N + 1];
int cnt[4], pcnt[4];
struct st {
    int p;
    bool ch;
    char f, s;
}a[3][MAX_N];
struct st2 {
    int p, ch;
    char x, y, z;
}b[MAX_N];
int main() {
    scanf("%s %s %s", str[0], str[1], str[2]);
    for (int i = 0; str[0][i]; i++) {
        char x = str[0][i], y = str[1][i], z = str[2][i];
        if (x == y && y == z) r[i] = x;
        else if (y == z) a[0][cnt[0]++] = { i,0,y,x };
        else if (x == z) a[1][cnt[1]++] = { i,0,x,y };
        else if (x == y) a[2][cnt[2]++] = { i,0,x,z };
        else b[cnt[3]++] = { i,0,x,y,z };
    }
    for (int i = 0; i<4; i++) pcnt[i] = cnt[i];
    for (int i = 0; i<pcnt[3]; i++) {
        int pos = max_element(pcnt, pcnt + 3) - pcnt;
        for (int j = 0; j<3; j++) if (j != pos) pcnt[j]++;
        b[i].ch = pos;
    }
    int tcnt = 0;
    while (1) {
        int mx1 = -1, mx2 = -1, t1, t2;
        for (int i = 0; i<3; i++)if (mx1<pcnt[i])mx1 = pcnt[i], t1 = i;
        for (int i = 0; i<3; i++)if (i != t1 && mx2<pcnt[i]) mx2 = pcnt[i], t2 = i;
        if (mx2>mx1 - 2) break;
        a[t1][tcnt++].ch = true;
        pcnt[t1]--;
        pcnt[t2]++;
        pcnt[3 - t1 - t2]++;
    }
    for (int i = 0; i<3; i++)
        for (int j = 0; j<cnt[i]; j++) r[a[i][j].p] = a[i][j].ch ? a[i][j].s : a[i][j].f;
    for (int i = 0; i<cnt[3]; i++)
        if (!b[i].ch) r[b[i].p] = b[i].x;
        else if (b[i].ch == 1) r[b[i].p] = b[i].y;
        else r[b[i].p] = b[i].z;
        printf("%d\n", *max_element(pcnt, pcnt + 3));
        puts(r);
        return 0;
}

댓글 없음 :

댓글 쓰기