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. // 처음엔 숭돌이 답 위주로
  17. for (int i = 0; i < N; i++) {
  18. ans[i] = SS[i];
  19. }
  20.  
  21. auto recompute = [&]() {
  22. scoreS = scoreK = scoreH = 0;
  23. for (int i = 0; i < N; i++) {
  24. if (ans[i] == SS[i]) scoreS++;
  25. if (ans[i] == SK[i]) scoreK++;
  26. if (ans[i] == SH[i]) scoreH++;
  27. }
  28. };
  29.  
  30. recompute();
  31.  
  32. // 안 되면 조정
  33. for (int i = 0; i < N; i++) {
  34. if (scoreS > scoreK && scoreK > scoreH) break;
  35. // 이 자리를 바꿔서 조정 시도
  36. char best = ans[i];
  37. int bestS = scoreS, bestK = scoreK, bestH = scoreH;
  38. for (char c : {SS[i], SK[i], SH[i], 'a'}) {
  39. if (c == ans[i]) continue;
  40. int oldS = (ans[i] == SS[i]);
  41. int oldK = (ans[i] == SK[i]);
  42. int oldH = (ans[i] == SH[i]);
  43. int newS = scoreS - oldS + (c == SS[i]);
  44. int newK = scoreK - oldK + (c == SK[i]);
  45. int newH = scoreH - oldH + (c == SH[i]);
  46. if (newS > newK && newK > newH) {
  47. best = c;
  48. bestS = newS; bestK = newK; bestH = newH;
  49. break;
  50. }
  51. }
  52. if (best != ans[i]) {
  53. ans[i] = best;
  54. scoreS = bestS; scoreK = bestK; scoreH = bestH;
  55. }
  56. }
  57.  
  58. if (scoreS > scoreK && scoreK > scoreH) {
  59. cout << ans << "\n";
  60. } else {
  61. cout << -1 << "\n";
  62. }
  63. }
Success #stdin #stdout 0.01s 5292KB
stdin
5
abcde
efgtb
abgte
stdout
-1