Advertisement
limimage

fixed

Apr 12th, 2020
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. #define endl "\n"
  4. using namespace std;
  5. using ll = long long;
  6. using ld = long double;
  7. using pii = pair<int, int>;
  8.  
  9. constexpr int N = 1e2 + 5;
  10.  
  11. string s;
  12. int ans = 0;
  13. int n, id;
  14. int dp[N][N];
  15.  
  16. int get(int i) {
  17. fill(&dp[0][0], &dp[N - 1][N], 0);
  18. string l = s.substr(0, i);
  19. string r = s.substr(i, n - i);
  20. reverse(r.begin(), r.end());
  21. for (int i = 1; i <= l.size(); i++) {
  22. for (int j = 1; j <= r.size(); j++)
  23. dp[i][j] = max({dp[i - 1][j - 1] + (l[i - 1] == r[j - 1]), dp[i][j - 1], dp[i - 1][j]});
  24. }
  25. return dp[l.size()][r.size()];
  26. }
  27.  
  28. void Solve() {
  29. cin >> s;
  30. n = s.size();
  31. for (int i = 1; i < n - 1; i++) {
  32. int v = get(i);
  33. if (ans < v) {
  34. ans = v;
  35. id = i;
  36. }
  37. }
  38. string answer;
  39. string dop;
  40. get(id);
  41. for (int i = id, j = n - id; i && j; ) {
  42. if (dp[i][j] == dp[i - 1][j]) {
  43. if (i == id)
  44. dop = string(1, s[i - 1]);
  45. i--;
  46. }
  47. else if (dp[i][j] == dp[i][j - 1]) {
  48. if (j == n - id)
  49. dop = string(1, s[id]);
  50. j--;
  51. }
  52.  
  53. else {
  54. i--;
  55. j--;
  56. answer.push_back(s[i]);
  57. }
  58. }
  59. string cp = answer;
  60. reverse(cp.begin(), cp.end());
  61. answer = cp + dop + answer;
  62. if (answer.size() == 0) {
  63. cout << 1 << endl << s.front();
  64. return;
  65. }
  66. cout << answer.size() << endl << answer;
  67. }
  68.  
  69. int main() {
  70. ios::sync_with_stdio(false);
  71. cin.tie(nullptr);
  72. cout.tie(nullptr);
  73. // cin >> t;
  74. // while(t--)
  75. //auto start = chrono::high_resolution_clock::now();
  76. Solve();
  77. //auto end = chrono::high_resolution_clock::now();
  78. //cout << endl << (chrono::duration_cast<chrono::duration<double>>(end - start)).count();
  79. return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement