Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <algorithm>
- using namespace std;
- int dp[1000][1000];
- int main() {
- ifstream fin("input.txt");
- ofstream fout("output.txt");
- string str;
- fin >> str;
- for (int i = 0; i < str.size(); i++) dp[i][i] = 1;
- for (int len = 1; len < str.size(); len++)
- for (int beg = 0; beg < str.size(); beg++) {
- if (beg + len < str.size() && str[beg] == str[beg + len])
- dp[beg][beg + len] = max(dp[beg][beg + len], dp[beg + 1][beg + len - 1] + 2);
- dp[beg][beg + len] = max(dp[beg][beg + len], dp[beg + 1][beg + len]);
- dp[beg][beg + len] = max(dp[beg][beg + len], dp[beg][beg + len - 1]);
- }
- fout << dp[0][str.size() - 1] << endl;
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment