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. if(!(cin >> N)) return 0;
  10. string SS, SK, SH;
  11. cin >> SS >> SK >> SH;
  12.  
  13. vector<int> idxC, idxBKH, idxBSH;
  14. idxC.reserve(N); idxBKH.reserve(N); idxBSH.reserve(N);
  15.  
  16. for (int i = 0; i < N; ++i) {
  17. char a = SS[i], b = SK[i], c = SH[i];
  18. if (a == b && b == c) {
  19. // A형: 모두 같음 -> 건너뜀(아무도 점수 안 주는 문자로 처리)
  20. } else if (a == b && b != c) {
  21. // B_SK형: S=K!=H (이번 구성에선 사용 안 함)
  22. } else if (a == c && c != b) {
  23. // B_SH형: S=H!=K -> K-only 가능
  24. idxBSH.push_back(i);
  25. } else if (b == c && c != a) {
  26. // B_KH형: K=H!=S -> S-only 가능
  27. idxBKH.push_back(i);
  28. } else {
  29. // C형: 모두 다름 -> S-only도 K-only도 가능(하지만 1자리는 1회만 사용)
  30. idxC.push_back(i);
  31. }
  32. }
  33.  
  34. // 필요 수량
  35. int needS = 2; // S-only 최소 2
  36. int needK = 1; // K-only 최소 1
  37.  
  38. // 먼저 B_KH로 S-only 충당
  39. int takeS_from_BKH = min(needS, (int)idxBKH.size());
  40. needS -= takeS_from_BKH;
  41.  
  42. // B_SH로 K-only 충당
  43. int takeK_from_BSH = min(needK, (int)idxBSH.size());
  44. needK -= takeK_from_BSH;
  45.  
  46. // 남은 수량은 C형에서 충당해야 함(서로 다른 인덱스로)
  47. if ((int)idxC.size() < needS + needK) {
  48. cout << -1 << "\n";
  49. return 0;
  50. }
  51.  
  52. // ---- 구성 시작 ----
  53. string S(N, 'a');
  54. // 우선 아무도 맞지 않게 각 자리마다 3글자와 다른 문자를 넣는다
  55. for (int i = 0; i < N; ++i) {
  56. char a = SS[i], b = SK[i], c = SH[i];
  57. char pick = 'a';
  58. if (pick == a || pick == b || pick == c) {
  59. for (char ch = 'a'; ch <= 'z'; ++ch) {
  60. if (ch != a && ch != b && ch != c) {
  61. pick = ch;
  62. break;
  63. }
  64. }
  65. }
  66. // 항상 존재(최대 3글자만 금지)
  67. S[i] = pick;
  68. }
  69.  
  70. vector<char> used(N, 0);
  71.  
  72. // S-only: 먼저 B_KH에서 사용
  73. vector<int> usedS, usedK;
  74. for (int i = 0; i < takeS_from_BKH; ++i) {
  75. int p = idxBKH[i];
  76. S[p] = SS[p]; // S만 +1
  77. used[p] = 1;
  78. usedS.push_back(p);
  79. }
  80.  
  81. // K-only: B_SH에서 사용
  82. for (int i = 0; i < takeK_from_BSH; ++i) {
  83. int p = idxBSH[i];
  84. S[p] = SK[p]; // K만 +1
  85. used[p] = 1;
  86. usedK.push_back(p);
  87. }
  88.  
  89. // 남은 S-only를 C형에서 확보
  90. int ci = 0;
  91. while (needS > 0 && ci < (int)idxC.size()) {
  92. int p = idxC[ci++];
  93. if (used[p]) continue;
  94. S[p] = SS[p]; // S만 +1
  95. used[p] = 1;
  96. usedS.push_back(p);
  97. --needS;
  98. }
  99.  
  100. // 남은 K-only를 C형에서 확보 (S-only로 쓴 C형과 겹치지 않게)
  101. while (needK > 0 && ci < (int)idxC.size()) {
  102. int p = idxC[ci++];
  103. if (used[p]) continue;
  104. S[p] = SK[p]; // K만 +1
  105. used[p] = 1;
  106. usedK.push_back(p);
  107. --needK;
  108. }
  109.  
  110. // 혹시 남았으면 더 찾아본다(이론상 위 체크로 여기 올 일 없음)
  111. for (; needS > 0 && ci < (int)idxC.size(); ++ci) {
  112. int p = idxC[ci];
  113. if (used[p]) continue;
  114. S[p] = SS[p];
  115. used[p] = 1;
  116. --needS;
  117. }
  118. for (; needK > 0 && ci < (int)idxC.size(); ++ci) {
  119. int p = idxC[ci];
  120. if (used[p]) continue;
  121. S[p] = SK[p];
  122. used[p] = 1;
  123. --needK;
  124. }
  125.  
  126. if (needS > 0 || needK > 0) {
  127. cout << -1 << "\n";
  128. return 0;
  129. }
  130.  
  131. cout << S << "\n";
  132. return 0;
  133. }
Success #stdin #stdout 0.01s 5316KB
stdin
5
abcde
efgtb
abgte
stdout
eacda