Advertisement
Guest User

Untitled

a guest
May 25th, 2016
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. using namespace std;
  6.  
  7. const int N = 1010;
  8. const int L = 45;
  9. char a[N][L];
  10. char s[L];
  11. int b[L];
  12. int n, m;
  13.  
  14. bool check()
  15. {
  16.     for (int i = 0; i < n; i++)
  17.     {
  18.         int cnt = 0;
  19.         for (int j = 0; j < m; j++)
  20.             cnt += (int)(s[j] != a[i][j]);
  21.         if (cnt >= 2) return false;
  22.     }
  23.     return true;
  24. }
  25.  
  26. int main()
  27. {
  28.     scanf("%d", &n);
  29.     for (int i = 0; i < n; i++)
  30.         scanf(" %s ", a[i]);
  31.     m = strlen(a[0]);
  32.     s[m] = (char)0;
  33.     for (int i = 0; i < n; i++)
  34.         for (int j = 0; j < m; j++)
  35.         {
  36.             if (a[i][j] == s[j] || b[j] == 0)
  37.             {
  38.                 s[j] = a[i][j];
  39.                 b[j]++;
  40.             }
  41.             else
  42.                 b[j]--;
  43.         }
  44.     for (int i = 0; i < n; i++)
  45.     {
  46.         int cnt = 0;
  47.         for (int j = 0; j < m; j++)
  48.             cnt += (int)(s[j] != a[i][j]);
  49.         if (cnt <= 1) continue;
  50.         if (cnt != 2) throw;
  51.         for (int j = 0; j < m; j++)
  52.         {
  53.             if (s[j] == a[i][j]) continue;
  54.             char old = s[j];
  55.             s[j] = a[i][j];
  56.             if (check())
  57.             {
  58.                 printf("%s\n", s);
  59.                 return 0;
  60.             }
  61.             s[j] = old;
  62.         }
  63.         throw;
  64.     }
  65.     printf("%s\n", s);
  66.  
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement