Advertisement
limimage

task5

Apr 12th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 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. get(id);
  40. for (int i = id, j = n - id; i && j; ) {
  41. if (dp[i][j] == dp[i - 1][j])
  42. i--;
  43. else if (dp[i][j] == dp[i][j - 1])
  44. j--;
  45. else {
  46. i--;
  47. j--;
  48. answer.push_back(s[i]);
  49. }
  50. }
  51. string cp = answer;
  52. reverse(cp.begin(), cp.end());
  53. answer = cp + answer;
  54. cout << answer.size() << endl << answer;
  55. }
  56.  
  57. int main() {
  58. ios::sync_with_stdio(false);
  59. cin.tie(nullptr);
  60. cout.tie(nullptr);
  61. // cin >> t;
  62. // while(t--)
  63. //auto start = chrono::high_resolution_clock::now();
  64. Solve();
  65. //auto end = chrono::high_resolution_clock::now();
  66. //cout << endl << (chrono::duration_cast<chrono::duration<double>>(end - start)).count();
  67. return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement