Tranvick

Z-1

Sep 21st, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. const int N = 111;
  8. string s;
  9. char ans[N];
  10. int f[N][N],a[N];
  11.  
  12.  
  13. int rec(int l,int r) {
  14.     if (l > r) return 0;
  15.     if (!f[l][r])
  16.         if (s[l]==s[r]) f[l][r] = rec(l + 1, r - 1) + 2 - (l == r);
  17.         else f[l][r] = max(rec(l + 1, r), rec(l, r - 1));
  18.     return f[l][r];
  19. }
  20. void print(int l, int r, int L, int R) {
  21.     while (l <= r) {
  22.         if (l == r && f[l][r] == 1) {
  23.             ans[L] = s[l];
  24.             ++L;
  25.             ++l;
  26.         } else if (s[l] == s[r]) {
  27.             ans[L] = s[l];
  28.             ++L;
  29.             ++l;
  30.             ans[R] = s[r];
  31.             --r;
  32.             --R;
  33.         } else {
  34.             if (f[l + 1][r] > f[l][r - 1]) ++l;
  35.             else r--;
  36.         }
  37.     }
  38. }
  39.  
  40. int main(){            
  41.     ifstream fin("input.txt");
  42.     ofstream fout("output.txt");
  43.     fin >> s;
  44.     int n = s.length();
  45.     rec(0, n - 1);
  46.     print(0, n - 1, 0, f[0][n - 1] - 1);
  47.     fout << f[0][n - 1] << endl << ans;
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment