Advertisement
999ms

Untitled

Apr 12th, 2020
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using i32 = int32_t;
  4. using ui32 = uint32_t;
  5. using i64 = int64_t;
  6. using ui64 = uint64_t;
  7.  
  8. using namespace std;
  9.  
  10. const int N = 101;
  11.  
  12. int dp[N][N];
  13. pair<int,int> from[N][N];
  14.  
  15. i32 main() {
  16.   ios_base::sync_with_stdio(false);
  17.   cin.tie(nullptr);
  18.   cout.tie(nullptr);
  19.   string s;
  20.   cin >> s;
  21.   const int n = s.size();
  22.   for (int i = 0; i < n; i++) {
  23.     for (int j = 0; j < n; j++) {
  24.       from[i][j] = pair<int,int>(-1,-1);
  25.     }
  26.   }
  27.  
  28.   for (int len = 1; len <= n; len++) {
  29.     for (int l = 0; l + len <= n; l++) {
  30.       int r = l + len - 1;
  31.       if (s[l] != s[r]) continue;
  32.       if (len <= 2) {
  33.         dp[l][r] = len;
  34.       } else {
  35.         for (int l1 = l + 1; l1 < r; l1++) {
  36.           for (int r1 = l1; r1 < r; r1++) {
  37.             if (s[l1] == s[r1] && dp[l1][r1] + 1 > dp[l][r]) {
  38.               dp[l][r] = dp[l1][r1] + 2;
  39.               from[l][r] = {l1, r1};
  40.             }
  41.           }
  42.         }
  43.       }
  44.     }
  45.   }
  46.  
  47.   int mx = 0;
  48.   pair<int,int> ind = {-1,-1};
  49.   for (int l = 0; l < n; l++) {
  50.     for (int r = l; r < n; r++) {
  51.       if (dp[l][r] > mx) {
  52.         ind = {l, r};
  53.         mx = dp[l][r];
  54.       }
  55.     }
  56.   }
  57.   string prefix = "";
  58.   string suffix = "";
  59.   while (ind != pair<int,int>(-1,-1)) {
  60.     if (ind.first == ind.second) {
  61.       prefix.push_back(s[ind.first]);
  62.       ind = from[ind.first][ind.second];
  63.     } else {
  64.       prefix.push_back(s[ind.first]);
  65.       suffix.push_back(s[ind.second]);
  66.       ind = from[ind.first][ind.second];
  67.     }
  68.   }
  69.   reverse(suffix.begin(), suffix.end());
  70.   string answer = prefix + suffix;
  71.   cout << answer.size() << endl;
  72.   cout << answer << endl;
  73.   return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement