Cod sursa(job #639918)

Utilizator mottyMatei-Dan Epure motty Data 24 noiembrie 2011 13:01:38
Problema PalM Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

const int N = 505;

int n;
char s[N];
int res;

int GetPal(int lf, int rt)
{
	if (lf != rt)
		return 1;
	int rAct = rt - lf + 1;
	char mx = s[lf];

	while (lf > 1 && rt < n && s[lf-1] == s[rt+1])
	{
		--lf;
		++rt;
		if (s[lf] < mx)
			rAct = rt - lf + 1;
		else
			mx = s[lf];
	}

	return rAct;
}

int SolveA()
{
	int rAct = 1;

	for (int i = 1; i <= n; ++i)
	{
		int x = GetPal(i, i);
		if (rAct < x)
			rAct = x;
	}

	return rAct;
}

int SolveB()
{
	int rAct = 1;

	for (int i = 1; i < n; ++i)
	{
		int x = GetPal(i, i+1);
		if (rAct < x)
			rAct = x;
	}

	return rAct;
}

void Solve()
{
	int a = SolveA();
	int b = SolveB();

	res = (a>b ? a:b);
}

int main()
{
	freopen("palm.in", "r", stdin);
	freopen("palm.out", "w", stdout);

	fgets(s+1, sizeof(s), stdin);
	n = strlen(s+1);

	Solve();
	printf("%d\n", res);

	return 0;
}