Cod sursa(job #637158)

Utilizator mihai_floreaFlorea Mihai Alexandru mihai_florea Data 20 noiembrie 2011 12:27:40
Problema PalM Scor 100
Compilator cpp Status done
Runda .com 2011 Marime 0.9 kb
#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;
}