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 S = SS;
  14. int scoreS = 0, scoreK = 0, scoreH = 0;
  15.  
  16. for (int i = 0; i < N; i++) {
  17. if (S[i] == SS[i]) scoreS++;
  18. if (S[i] == SK[i]) scoreK++;
  19. if (S[i] == SH[i]) scoreH++;
  20. }
  21.  
  22. auto check = [&](int a, int b, int c) {
  23. return (a > b && b > c);
  24. };
  25.  
  26. if (check(scoreS, scoreK, scoreH)) {
  27. cout << S << "\n";
  28. return 0;
  29. }
  30.  
  31. // 여러 자리를 고쳐야 할 수도 있으니 전체 탐색
  32. for (int i = 0; i < N; i++) {
  33. int oldS = (S[i] == SS[i]);
  34. int oldK = (S[i] == SK[i]);
  35. int oldH = (S[i] == SH[i]);
  36.  
  37. // 후보 1: 고돌이 답 채택 (단, 한돌이는 틀리게)
  38. if (SK[i] != SH[i]) {
  39. int tmpS = scoreS - oldS + (SK[i] == SS[i]);
  40. int tmpK = scoreK - oldK + 1;
  41. int tmpH = scoreH - oldH + (SK[i] == SH[i] ? 1 : 0);
  42.  
  43. if (tmpS > tmpK && tmpK > tmpH) {
  44. S[i] = SK[i];
  45. cout << S << "\n";
  46. return 0;
  47. }
  48.  
  49. // 일단 바꿔서 점수 저장
  50. scoreS = tmpS;
  51. scoreK = tmpK;
  52. scoreH = tmpH;
  53. S[i] = SK[i];
  54. }
  55.  
  56. // 후보 2: 셋 다 틀리게 (제3 문자 선택)
  57. if (!check(scoreS, scoreK, scoreH)) {
  58. int tmpS = scoreS - (S[i] == SS[i]);
  59. int tmpK = scoreK - (S[i] == SK[i]);
  60. int tmpH = scoreH - (S[i] == SH[i]);
  61. if (check(tmpS, tmpK, tmpH)) {
  62. for (char c = 'a'; c <= 'z'; c++) {
  63. if (c != SS[i] && c != SK[i] && c != SH[i]) {
  64. S[i] = c;
  65. break;
  66. }
  67. }
  68. cout << S << "\n";
  69. return 0;
  70. }
  71. }
  72. }
  73.  
  74. if (check(scoreS, scoreK, scoreH)) {
  75. cout << S << "\n";
  76. } else {
  77. cout << -1 << "\n";
  78. }
  79. return 0;
  80. }
Success #stdin #stdout 0.01s 5328KB
stdin
5
aaaaa
aaaab
aaaac
stdout
-1