Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using i32 = int32_t;
- using ui32 = uint32_t;
- using i64 = int64_t;
- using ui64 = uint64_t;
- using namespace std;
- const int N = 101;
- int dp[N][N];
- pair<int,int> from[N][N];
- i32 main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- string s;
- cin >> s;
- const int n = s.size();
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- from[i][j] = pair<int,int>(-1,-1);
- }
- }
- for (int len = 1; len <= n; len++) {
- for (int l = 0; l + len <= n; l++) {
- int r = l + len - 1;
- if (s[l] != s[r]) continue;
- if (len <= 2) {
- dp[l][r] = len;
- } else {
- for (int l1 = l + 1; l1 < r; l1++) {
- for (int r1 = l1; r1 < r; r1++) {
- if (s[l1] == s[r1] && dp[l1][r1] + 1 > dp[l][r]) {
- dp[l][r] = dp[l1][r1] + 2;
- from[l][r] = {l1, r1};
- }
- }
- }
- }
- }
- }
- int mx = 0;
- pair<int,int> ind = {-1,-1};
- for (int l = 0; l < n; l++) {
- for (int r = l; r < n; r++) {
- if (dp[l][r] > mx) {
- ind = {l, r};
- mx = dp[l][r];
- }
- }
- }
- string prefix = "";
- string suffix = "";
- while (ind != pair<int,int>(-1,-1)) {
- if (ind.first == ind.second) {
- prefix.push_back(s[ind.first]);
- ind = from[ind.first][ind.second];
- } else {
- prefix.push_back(s[ind.first]);
- suffix.push_back(s[ind.second]);
- ind = from[ind.first][ind.second];
- }
- }
- reverse(suffix.begin(), suffix.end());
- string answer = prefix + suffix;
- cout << answer.size() << endl;
- cout << answer << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement