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.  
  15. // 우선 전부 숭돌이 답으로 시작
  16. for(int i=0;i<N;i++) ans[i]=SS[i];
  17.  
  18. auto score = [&](string &A){
  19. int s=0,k=0,h=0;
  20. for(int i=0;i<N;i++){
  21. if(A[i]==SS[i]) s++;
  22. if(A[i]==SK[i]) k++;
  23. if(A[i]==SH[i]) h++;
  24. }
  25. return tuple<int,int,int>(s,k,h);
  26. };
  27.  
  28. auto [s,k,h] = score(ans);
  29.  
  30. // 3명 다 같은 칸은 무조건 틀리게 처리
  31. for(int i=0;i<N;i++){
  32. if(SS[i]==SK[i] && SK[i]==SH[i]){
  33. char c='a';
  34. if(c==SS[i]) c='b';
  35. ans[i]=c;
  36. }
  37. }
  38. tie(s,k,h)=score(ans);
  39.  
  40. // 조건 될 때까지 조정
  41. bool progress=true;
  42. while(!(s>k && k>h) && progress){
  43. progress=false;
  44. for(int i=0;i<N;i++){
  45. int oldS=(ans[i]==SS[i]);
  46. int oldK=(ans[i]==SK[i]);
  47. int oldH=(ans[i]==SH[i]);
  48. for(char c : {SS[i],SK[i],SH[i],'a'}){
  49. if(c==ans[i]) continue;
  50. int ns=s-oldS+(c==SS[i]);
  51. int nk=k-oldK+(c==SK[i]);
  52. int nh=h-oldH+(c==SH[i]);
  53. if(ns>nk && nk>nh){
  54. ans[i]=c;
  55. s=ns; k=nk; h=nh;
  56. progress=true;
  57. break;
  58. }
  59. }
  60. if(progress) break;
  61. }
  62. }
  63.  
  64. if(s>k && k>h) cout<<ans<<"\n";
  65. else cout<<-1<<"\n";
  66. }
Success #stdin #stdout 0.01s 5320KB
stdin
5
abcde
efgtb
abgte
stdout
-1