Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <array>
- #include <algorithm>
- #include <numeric>
- #include <iostream>
- #include <vector>
- #include <map>
- #include <stack>
- #include <cassert>
- #include <iomanip>
- using namespace std;
- #define int long long
- const long long INF = 1e18 + 7;
- const int MAXN = 1e2 + 100;
- const int N = 1e5 + 10;
- array<array<int, MAXN>, MAXN> dp;
- string s;
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- cin >> s;
- int n = s.size();
- for (int i = 0; i < n; ++i) {
- dp[i][i] = 1;
- }
- for (int len = 1; len < n; ++len) {
- for (int l = 0; l + len < n; ++l) {
- int r = l + len;
- dp[l][r] = max({dp[l + 1][r], dp[l][r - 1], dp[l + 1][r - 1] + (s[l] == s[r] ? 2: 0)});
- }
- }
- cout << dp[0][n - 1] << '\n';
- string ans(n, '#');
- int l = 0, r = n - 1;
- while (l <= r) {
- if (l == r && dp[l][r] == 1) {
- ans[l] = s[l];
- ++l;
- continue;
- }
- if (s[l] == s[r]) {
- ans[l] = s[l];
- ans[r] = s[r];
- ++l;
- --r;
- } else {
- if (dp[l + 1][r] > dp[l][r - 1]) {
- ++l;
- } else {
- --r;
- }
- }
- }
- for (auto i: ans) {
- if (i != '#') {
- cout << i;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment