fork download
  1. #include<bits/stdc++.h>
  2. #define M 2004
  3. using namespace std;
  4.  
  5. string a, b;
  6. int dp[M][M];
  7.  
  8. int main()
  9. {
  10. ios_base::sync_with_stdio(0);
  11. cin.tie(0); cout.tie(0);
  12.  
  13. cin >> a;
  14. b = a;
  15. reverse(b.begin(), b.end());
  16. int n = a.size();
  17. a = ' ' + a;
  18. b = ' ' + b;
  19. for(int i = 1; i <= n; i++)
  20. {
  21. for(int j = 1; j <= n; j++)
  22. {
  23. if(a[i] == b[j]) dp[i][j] = dp[i - 1][j - 1] + 1;
  24. else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
  25. }
  26. }
  27. string ans = "";
  28. int x = dp[n][n];
  29. int m = n;
  30. while(1)
  31. {
  32. if(a[n] == b[m])
  33. {
  34. ans += a[n];
  35. n--;
  36. m--;
  37. }
  38. else
  39. {
  40. if(dp[n][m] == dp[n - 1][m]) n--;
  41. else m--;
  42. }
  43. if(ans.size() == x) break;
  44. }
  45. cout << ans;
  46. }
  47.  
Success #stdin #stdout 0s 5288KB
stdin
lmevxeyzl
stdout
level