Created
July 10, 2014 12:05
-
-
Save kumar8600/4b162d411f4095e78438 to your computer and use it in GitHub Desktop.
JAG Asia 2006 Problem F: It Prefokery Pio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
#define FOR(i,a,b) for(int i=(a);i<(int)(b);++i) | |
#define RALL(a) (a).rbegin(),(a).rend() | |
typedef vector<int> vi; | |
typedef vector<vi> vvi; | |
string reverseLcs(string & a, string & b, vvi & dp, size_t i, size_t j) | |
{ | |
if (i == 0 || j == 0) { | |
return ""; | |
} | |
if (a.at(i - 1) == b.at(j - 1)) { | |
return reverseLcs(a, b, dp, i - 1, j - 1) + a.at(i - 1); | |
} else { | |
if (dp[i - 1][j] == dp[i][j]) { | |
return reverseLcs(a, b, dp, i - 1, j); | |
} else { | |
return reverseLcs(a, b, dp, i, j - 1); | |
} | |
} | |
} | |
string lcs(string & a, string & b) | |
{ | |
vvi dp(a.size() + 1, vi(b.size() + 1)); | |
FOR(i, 1, dp.size()) { | |
FOR(j, 1, dp[0].size()) { | |
if (a.at(i - 1) == b.at(j - 1)) { | |
dp[i][j] = dp[i - 1][j - 1] + 1; | |
} else { | |
dp[i][j] = max(dp[i - 1][j], | |
dp[i][j - 1]); | |
} | |
} | |
} | |
return reverseLcs(a, b, dp, a.length(), b.length()); | |
} | |
int main() | |
{ | |
string str; | |
while (cin >> str) { | |
string rstr(RALL(str)); | |
cout << lcs(str, rstr) << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment