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 S_S, S_K, S_H;
  11. cin >> S_S >> S_K >> S_H;
  12.  
  13. string S = S_S; // 처음엔 숭돌이 답안
  14.  
  15. // 초기 점수 계산
  16. int scoreS = 0, scoreK = 0, scoreH = 0;
  17. for (int i = 0; i < N; i++) {
  18. if (S[i] == S_S[i]) scoreS++;
  19. if (S[i] == S_K[i]) scoreK++;
  20. if (S[i] == S_H[i]) scoreH++;
  21. }
  22.  
  23. // 바로 조건을 만족하면 출력
  24. if (scoreS > scoreK && scoreK > scoreH) {
  25. cout << S << "\n";
  26. return 0;
  27. }
  28.  
  29. // 한 위치를 바꿔보면서 확인
  30. for (int i = 0; i < N; i++) {
  31. char orig = S[i];
  32. // 후보: S_K[i], S_H[i], 또는 아무도 아닌 다른 문자
  33. vector<char> candidates = {S_K[i], S_H[i]};
  34. for (char c = 'a'; c <= 'z'; c++) {
  35. if (c != S_S[i] && c != S_K[i] && c != S_H[i]) {
  36. candidates.push_back(c);
  37. break; // 하나만 있으면 충분
  38. }
  39. }
  40.  
  41. for (char cand : candidates) {
  42. S[i] = cand;
  43.  
  44. int s = 0, k = 0, h = 0;
  45. for (int j = 0; j < N; j++) {
  46. if (S[j] == S_S[j]) s++;
  47. if (S[j] == S_K[j]) k++;
  48. if (S[j] == S_H[j]) h++;
  49. }
  50.  
  51. if (s > k && k > h) {
  52. cout << S << "\n";
  53. return 0;
  54. }
  55. }
  56.  
  57. S[i] = orig; // 복원
  58. }
  59.  
  60. cout << -1 << "\n";
  61. return 0;
  62. }
Success #stdin #stdout 0.01s 5316KB
stdin
5
abcde
efgtb
abgte
stdout
-1