Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- const int N = 111;
- string s;
- char ans[N];
- int f[N][N],a[N];
- int rec(int l,int r) {
- if (l > r) return 0;
- if (!f[l][r])
- if (s[l]==s[r]) f[l][r] = rec(l + 1, r - 1) + 2 - (l == r);
- else f[l][r] = max(rec(l + 1, r), rec(l, r - 1));
- return f[l][r];
- }
- void print(int l, int r, int L, int R) {
- while (l <= r) {
- if (l == r && f[l][r] == 1) {
- ans[L] = s[l];
- ++L;
- ++l;
- } else if (s[l] == s[r]) {
- ans[L] = s[l];
- ++L;
- ++l;
- ans[R] = s[r];
- --r;
- --R;
- } else {
- if (f[l + 1][r] > f[l][r - 1]) ++l;
- else r--;
- }
- }
- }
- int main(){
- ifstream fin("input.txt");
- ofstream fout("output.txt");
- fin >> s;
- int n = s.length();
- rec(0, n - 1);
- print(0, n - 1, 0, f[0][n - 1] - 1);
- fout << f[0][n - 1] << endl << ans;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment