fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7.  
  8. int N;
  9. cin >> N;
  10. string SS, SK, SH;
  11. cin >> SS >> SK >> SH;
  12.  
  13. string ans(N, 'a');
  14. int scoreS = 0, scoreK = 0, scoreH = 0;
  15.  
  16. for (int i = 0; i < N; i++) {
  17. // 세 명 답이 전부 같은 경우 → 모두 맞추면 동점 → 다른 글자 선택
  18. if (SS[i] == SK[i] && SK[i] == SH[i]) {
  19. char c = 'a';
  20. if (c == SS[i]) c = (c == 'a' ? 'b' : 'a');
  21. ans[i] = c; // 아무도 점수 없음
  22. } else {
  23. // 우선 숭돌이 답으로 선택
  24. ans[i] = SS[i];
  25. if (ans[i] == SS[i]) scoreS++;
  26. if (ans[i] == SK[i]) scoreK++;
  27. if (ans[i] == SH[i]) scoreH++;
  28. }
  29. }
  30.  
  31. // 만약 최종 점수가 조건을 만족 못 하면,
  32. // 고돌이 점수를 일부러 올려주는 시도
  33. if (!(scoreS > scoreK && scoreK > scoreH)) {
  34. for (int i = 0; i < N; i++) {
  35. // 현재 선택이 숭돌이 답인데, 고돌이 답과 다르면
  36. if (ans[i] == SS[i] && SS[i] != SK[i]) {
  37. // 숭돌이 점수 1 줄이고 고돌이 점수 1 늘려보기
  38. ans[i] = SK[i];
  39. scoreS--; scoreK++;
  40. if (ans[i] == SH[i]) scoreH++;
  41. if (scoreS > scoreK && scoreK > scoreH) break;
  42. // 복원 (조건 안되면 다시 원래대로)
  43. if (!(scoreS > scoreK && scoreK > scoreH)) {
  44. if (ans[i] == SH[i]) scoreH--;
  45. ans[i] = SS[i];
  46. scoreS++; scoreK--;
  47. }
  48. }
  49. }
  50. }
  51.  
  52. if (scoreS > scoreK && scoreK > scoreH) {
  53. cout << ans << "\n";
  54. } else {
  55. cout << -1 << "\n";
  56. }
  57. }
Success #stdin #stdout 0s 5320KB
stdin
5
abcde
efgtb
abgte
stdout
-1