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. // 1차: 숭돌이 답 위주로 채움
  17. for (int i = 0; i < N; i++) {
  18. if (SS[i] == SK[i] && SK[i] == SH[i]) {
  19. // 모두 같으면 틀리게
  20. char c = 'a';
  21. if (c == SS[i]) c = 'b';
  22. ans[i] = c;
  23. } else {
  24. ans[i] = SS[i];
  25. }
  26. }
  27.  
  28. // 점수 계산
  29. for (int i = 0; i < N; i++) {
  30. if (ans[i] == SS[i]) scoreS++;
  31. if (ans[i] == SK[i]) scoreK++;
  32. if (ans[i] == SH[i]) scoreH++;
  33. }
  34.  
  35. // 조건 만족 못 하면 고쳐야 함
  36. if (!(scoreS > scoreK && scoreK > scoreH)) {
  37. bool fixed = false;
  38. for (int i = 0; i < N && !fixed; i++) {
  39. // 다른 글자로 바꿔서 조정 시도
  40. int oldS = (ans[i] == SS[i]);
  41. int oldK = (ans[i] == SK[i]);
  42. int oldH = (ans[i] == SH[i]);
  43.  
  44. for (char c = 'a'; c <= 'z'; c++) {
  45. if (c == ans[i]) continue;
  46. int nS = (c == SS[i]);
  47. int nK = (c == SK[i]);
  48. int nH = (c == SH[i]);
  49. int newS = scoreS - oldS + nS;
  50. int newK = scoreK - oldK + nK;
  51. int newH = scoreH - oldH + nH;
  52. if (newS > newK && newK > newH) {
  53. ans[i] = c;
  54. scoreS = newS;
  55. scoreK = newK;
  56. scoreH = newH;
  57. fixed = true;
  58. break;
  59. }
  60. }
  61. }
  62. }
  63.  
  64. if (scoreS > scoreK && scoreK > scoreH) {
  65. cout << ans << "\n";
  66. } else {
  67. cout << -1 << "\n";
  68. }
  69. }
Success #stdin #stdout 0s 5324KB
stdin
5
abcde
efgtb
abgte
stdout
-1