Raslboyy

3130

Sep 24th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. int dp[1000][1000];
  9.  
  10. int main() {
  11.     ifstream fin("input.txt");
  12.     ofstream fout("output.txt");
  13.  
  14.     string str;
  15.     fin >> str;
  16.     for (int i = 0; i < str.size(); i++) dp[i][i] = 1;
  17.     for (int len = 1; len < str.size(); len++)
  18.         for (int beg = 0; beg < str.size(); beg++) {
  19.             if (beg + len < str.size() && str[beg] == str[beg + len])
  20.                 dp[beg][beg + len] = max(dp[beg][beg + len], dp[beg + 1][beg + len - 1] + 2);
  21.             dp[beg][beg + len] = max(dp[beg][beg + len], dp[beg + 1][beg + len]);
  22.             dp[beg][beg + len] = max(dp[beg][beg + len], dp[beg][beg + len - 1]);
  23.         }
  24.    
  25.     fout << dp[0][str.size() - 1] << endl;
  26.    
  27.     fin.close();
  28.     fout.close();
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment