Cod sursa(job #636838)

Utilizator crushackPopescu Silviu crushack Data 20 noiembrie 2011 00:13:20
Problema PalM Scor 70
Compilator cpp Status done
Runda .com 2011 Marime 0.7 kb
#include <stdio.h>
#include <string.h>
#define NMax 512

const char IN[]="palm.in",OUT[]="palm.out";

int N;
int T[NMax][NMax][32];
char s[NMax];

int max(int x,int y){
	return x>y ? x : y;
}

int main()
{
	int i,j,c,k;
	freopen(IN,"r",stdin);
	scanf("%s",s);
	fclose(stdin);

	N=strlen(s);

	for (i=0;i<N;++i)
		for (j=0;j+'a'<=s[i];++j)
			T[i][i][j]=1;

	for (k=1;k<=N;++k)
		for (i=0;i<N;++i)
			for (c='z'-'a';c>=0;--c)
			{
				j=i+k;
				T[i][j][c]= max ( max ( T[i][j-1][c] , T[i+1][j][c] ) , T[i][j][c+1] );
				if (s[i]==s[j] && s[j]==c+'a')
                    T[i][j][c]=max(T[i][j][c],T[i+1][j-1][c]+2);
			}

	freopen(OUT,"w",stdout);
	printf("%d\n",T[0][N-1][0]);
	fclose(stdout);

	return 0;
}