Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define endl "\n"
- using namespace std;
- using ll = long long;
- using ld = long double;
- using pii = pair<int, int>;
- constexpr int N = 1e2 + 5;
- string s;
- int ans = 0;
- int n, id;
- int dp[N][N];
- int get(int i) {
- fill(&dp[0][0], &dp[N - 1][N], 0);
- string l = s.substr(0, i);
- string r = s.substr(i, n - i);
- reverse(r.begin(), r.end());
- for (int i = 1; i <= l.size(); i++) {
- for (int j = 1; j <= r.size(); j++)
- dp[i][j] = max({dp[i - 1][j - 1] + (l[i - 1] == r[j - 1]), dp[i][j - 1], dp[i - 1][j]});
- }
- return dp[l.size()][r.size()];
- }
- void Solve() {
- cin >> s;
- n = s.size();
- for (int i = 1; i < n - 1; i++) {
- int v = get(i);
- if (ans < v) {
- ans = v;
- id = i;
- }
- }
- string answer;
- get(id);
- for (int i = id, j = n - id; i && j; ) {
- if (dp[i][j] == dp[i - 1][j])
- i--;
- else if (dp[i][j] == dp[i][j - 1])
- j--;
- else {
- i--;
- j--;
- answer.push_back(s[i]);
- }
- }
- string cp = answer;
- reverse(cp.begin(), cp.end());
- answer = cp + answer;
- cout << answer.size() << endl << answer;
- }
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- // cin >> t;
- // while(t--)
- //auto start = chrono::high_resolution_clock::now();
- Solve();
- //auto end = chrono::high_resolution_clock::now();
- //cout << endl << (chrono::duration_cast<chrono::duration<double>>(end - start)).count();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement