Advertisement
Dang_Quan_10_Tin

XCKLT

Dec 14th, 2020 (edited)
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <stack>
  5. #include <algorithm>
  6. #define task "XCKLT"
  7. #define vt vector
  8.  
  9. using namespace std;
  10. using ll = long long;
  11.  
  12. const int N = 5e2 + 2;
  13. const int Inf = 1e9 + 7;
  14. string a, b, c;
  15. int m, n, p;
  16. int f[N * 2][N];
  17. bool can[N * 2][N];
  18.  
  19. void Read()
  20. {
  21.     cin >> a >> b >> c;
  22.     m = a.size();
  23.     n = b.size();
  24.     p = c.size();
  25. }
  26.  
  27. void Sub_1()
  28. {
  29.     if (a == c || b == c)
  30.     {
  31.         cout << "TRETRAU";
  32.         return;
  33.     }
  34.     if (a != b)
  35.         cout << "2\n"
  36.              << (a + b);
  37.     else
  38.         cout << "1\n"
  39.              << a;
  40. }
  41.  
  42. string Trace_2(vt<vt<vt<vt<char>>>> par, int i, int j, int t, int h)
  43. {
  44.     string s;
  45.     while (i != 0)
  46.     {
  47.         if (a[j] == par[i][j][t][h] || b[t] == par[i][j][t][h] || c[h] == par[i][j][t][h])
  48.         {
  49.             s.push_back(par[i][j][t][h]);
  50.             j -= a[j] == s.back();
  51.             t -= b[t] == s.back();
  52.             h -= c[h] == s.back();
  53.         }
  54.         --i;
  55.     }
  56.     reverse(s.begin(), s.end());
  57.     return s;
  58. }
  59.  
  60. int LCS(const string &s, const string &p)
  61. {
  62.     for (int i = 1; i <= (int)s.size(); ++i)
  63.         for (int j = 1; j <= (int)p.size(); ++j)
  64.         {
  65.             if (s[i - 1] == p[j - 1])
  66.                 f[i][j] = f[i - 1][j - 1] + 1;
  67.             else
  68.                 f[i][j] = max(f[i - 1][j], f[i][j - 1]);
  69.         }
  70.     return f[s.size()][p.size()];
  71. }
  72.  
  73. void Sub_2()
  74. {
  75.     a = " " + a;
  76.     b = " " + b;
  77.     c = " " + c;
  78.     vt<vt<vt<vt<bool>>>> dp(m + n + 1, vt<vt<vt<bool>>>(m + 1, vt<vt<bool>>(n + 1, vt<bool>(p + 1, 0))));
  79.     vt<vt<vt<vt<char>>>> par(m + n + 1, vt<vt<vt<char>>>(m + 1, vt<vt<char>>(n + 1, vt<char>(p + 1, '*'))));
  80.     dp[0][0][0][0] = 1;
  81.     for (int i = 1; i <= m + n; ++i)
  82.         for (int j = 0; j <= m; ++j)
  83.             for (int t = 0; t <= n; ++t)
  84.                 for (int h = 0; h <= p; ++h)
  85.                 {
  86.                     /*if(dp[i - 1][j][t][h]){
  87.                         dp[i][j][t][h] = 1;
  88.                         continue;
  89.                     }*/
  90.                     vector<char> u;
  91.                     if (j > 0)
  92.                         u.push_back(a[j]);
  93.                     if (t > 0)
  94.                         u.push_back(b[t]);
  95.                     if (h > 0)
  96.                         u.push_back(c[h]);
  97.                     for (char k : u)
  98.                     {
  99.                         register int x = j - (a[j] == k),
  100.                                      y = t - (b[t] == k),
  101.                                      z = h - (c[h] == k);
  102.                         if (dp[i - 1][x][y][z] && ((!dp[i - 1][x][y][h]) || h == p || c[h + 1] != k))
  103.                         {
  104.                             dp[i][j][t][h] = 1;
  105.                             par[i][j][t][h] = k;
  106.                             break;
  107.                         }
  108.                     }
  109.                 }
  110.     for (int i = 1; i <= m + n; ++i)
  111.     {
  112.         for (int j = 0; j < p; ++j)
  113.             if (dp[i][m][n][j])
  114.             {
  115.                 string s = Trace_2(par, i, m, n, j);
  116.                 if (LCS(s, c.substr(1, p)) < p)
  117.                 {
  118.                     cout << i << "\n"
  119.                          << s;
  120.                     return;
  121.                 }
  122.             }
  123.     }
  124.     cout << "TRETRAU";
  125. }
  126.  
  127. string Trace_3(vt<vt<vt<char>>> par, int j, int t, int h)
  128. {
  129.     string s;
  130.     while (j != 0 || t != 0 || h != 0)
  131.     {
  132.         if (a[j] == par[j][t][h] || b[t] == par[j][t][h] || c[h] == par[j][t][h])
  133.         {
  134.             s.push_back(par[j][t][h]);
  135.             j -= a[j] == s.back();
  136.             t -= b[t] == s.back();
  137.             h -= c[h] == s.back();
  138.         }
  139.     }
  140.     reverse(s.begin(), s.end());
  141.     return s;
  142. }
  143.  
  144. void Sub_3()
  145. {
  146.  
  147.     a = " " + a;
  148.     b = " " + b;
  149.     c = " " + c;
  150.     vt<vt<vt<int>>> dp(m + 1, vt<vt<int>>(n + 1, vt<int>(p + 1, Inf)));
  151.     vt<vt<vt<char>>> par(m + 1, vt<vt<char>>(n + 1, vt<char>(p + 1, '*')));
  152.     dp[0][0][0] = 0;
  153.     for (int j = 0; j <= m; ++j)
  154.         for (int t = 0; t <= n; ++t)
  155.             for (int h = 0; h <= p; ++h)
  156.             {
  157.                 vector<char> u;
  158.                 if (j > 0)
  159.                     u.push_back(a[j]);
  160.                 if (t > 0)
  161.                     u.push_back(b[t]);
  162.                 if (h > 0)
  163.                     u.push_back(c[h]);
  164.                 for (char k : u)
  165.                 {
  166.                     register int x = j - (a[j] == k),
  167.                                  y = t - (b[t] == k),
  168.                                  z = h - (c[h] == k);
  169.                     if (dp[j][t][h] > dp[x][y][z] + 1 && (dp[x][y][h] == Inf || h == p || c[h + 1] != k))
  170.                     {
  171.                         dp[j][t][h] = dp[x][y][z] + 1;
  172.                         par[j][t][h] = k;
  173.                     }
  174.                 }
  175.             }
  176.     for (int i = 0; i < p; ++i)
  177.         if (dp[m][n][i] != Inf)
  178.         {
  179.             string s = Trace_3(par, m, n, i);
  180.             if (LCS(s, c.substr(1, p)) < p)
  181.             {
  182.                 cout << dp[m][n][i] << "\n"
  183.                      << s;
  184.                 return;
  185.             }
  186.         }
  187.     cout << "TRETRAU";
  188. }
  189.  
  190. int32_t main()
  191. {
  192.     if (fopen(task ".INP", "r"))
  193.     {
  194.         freopen(task ".INP", "r", stdin);
  195.         freopen(task ".OUT", "w", stdout);
  196.     }
  197.     ios_base::sync_with_stdio(0);
  198.     cin.tie(0);
  199.     cout.tie(0);
  200.     Read();
  201.     if (a.size() == 1 && b.size() == 1 && c.size() == 1)
  202.         Sub_1();
  203.     else if (1ll * (m + n) * m * n * p * 26 <= 5e7)
  204.         Sub_2();
  205.     else
  206.         Sub_3();
  207. }
  208.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement