Cod sursa(job #37506)

Utilizator raula_sanChis Raoul raula_san Data 25 martie 2007 10:39:34
Problema Elimin 2 Scor 0
Compilator cpp Status done
Runda preONI 2007, Runda 4, Clasa a 10-a Marime 1.3 kb
# include <stdio.h>
# include <string>

using namespace std;

# define input "elimin2.in"
# define output "elimin2.out"

# define max 2003
# define maxp 4002005
long l[maxp],ant[maxp],n,i,j,t,poz,val,ma,rez[max],m;
int v[maxp],d[maxp];

char s[max];

int main()
{
	freopen(input,"r",stdin);
	freopen(output,"w",stdout);

	scanf("%d",&t);
	while(t--)
	{
		scanf("%s",&s);
		n = strlen(s);
		poz =0;
		for(i = 1;i<=n;++i)
		{
			for(j=n;j>=1;--j)
			{
				if(s[i-1] == s[j-1])
				{
					v[++poz] =i;
					d[poz]= j;
				}
			}
		}

		l[1] = 1;

		n=poz;
		for(i=2;i<=n;++i)
		{
			ant[i]=0;
			poz = 0;
				ma=1;

			for(j=i-1;j;--j)
			{
				if(v[i] > v[j] && d[i]<d[j] &&ma==l[j]+1)
				{
					if(s[v[j]-1] > s[v[poz]-1])
						poz = j;
				}
				if(v[i] > v[j]&& d[i]<d[j] && ma < l[j]+1)
				{
					ma = l[j]+1;
					poz = j;
				}
			}
			l[i] = ma;
			ant[i] = poz;
		}
		ma = 0;
		for(i=1;i<=n;i++)
		{
			if(l[i] == ma)
				if(s[v[poz]-1] < s[v[i]-1])
					poz = i;
			if(l[i] > ma)
				ma = l[i],poz=i;
		}
		m = 0;
		val = poz;
		while(poz)
		{
			rez[++m] = s[v[poz]-1]-'0';
			poz = ant[poz];
		}
		i=1;
		while(!rez[i] && i<=m)i++;
		poz = i-1;
			for(;i<=m-poz;++i)
				printf("%ld",rez[i]);

		printf("\n");
	}


	return 0;
}