Pagini recente » Cod sursa (job #2418117) | Cod sursa (job #2246942) | Cod sursa (job #132086) | Cod sursa (job #1571963) | Cod sursa (job #637158)
Cod sursa(job #637158)
#include <fstream>
#include <algorithm>
using namespace std;
const int NMAX = 505;
const int SIGMA = 26;
int dp[NMAX][NMAX], first[SIGMA], last[SIGMA];
string s;
int main()
{
ifstream fin("palm.in");
ofstream fout("palm.out");
getline(fin, s);
for (int j = 0; j < s.length(); ++j)
{
fill(first, first+SIGMA, -1);
fill(last, last+SIGMA, -1);
dp[j][j] = 1;
for (int i = j-1; i>=0; --i)
{
if (s[i] != s[j]) dp[i][j] = 0;
else
{
dp[i][j] = 2;
for (int c = s[i]-'a'; c < SIGMA; ++c)
{
if (first[c] == -1) continue;
dp[i][j] = max(dp[i][j], 2 + dp[first[c]][last[c]]);
}
}
if (last[s[i] - 'a'] == -1) last[s[i]-'a'] = i;
first[s[i]-'a'] = i;
}
}
int ans = 0;
for (int i = 0; i < s.length(); ++i)
for (int j=i; j < s.length(); ++j)
ans = max(ans, dp[i][j]);
fout<<ans<<"\n";
return 0;
}