Cod sursa(job #38948)

Utilizator mariusdrgdragus marius mariusdrg Data 26 martie 2007 11:57:02
Problema Elimin 2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.59 kb
#include<stdio.h>
#include<string.h>

const int maxn = 1000/*1*/;

char a[maxn];
int mat[maxn][maxn];
int it[maxn][maxn];
int i, j, nr;
int n;
int aux1, aux2;
int st1[maxn], st2[maxn];

int cmp(int x1,int y1, int x2, int y2)
{
    if (mat[x1][y1] > mat[x2][y2])
    {
        return 1;
    }
    if (mat[x1][y1] == mat[x2][y2] && a[it[x1][y1] / n] > a[it[x2][y2] / n])
    {
        return 1;
    }
    return 0;
}


int main()
{
    freopen("elimin2.in","r",stdin);
    freopen("elimin2.out","w",stdout);
    fgets(a,1000,stdin);
    n = strlen(a) - 1;
 /*   for(i = 1;i <= 2000; ++i)
    {
    	for(j = 1;j <= 2000; ++j)
        {
        	mat[i][j] = 1;
            it[i][j] = 1;
        }
    }*/
    for(i = 0;i < n; ++i)
    {
    	for(j = 0;j < n - i; j++)
        {
        	nr = 0;
			it[i][j] = -1;
        	if (a[i] == a[n - j - 1])
            {
            	nr = 2;
            	if (i == n - j - 1)
                {
                	nr = 1;
                }

            	if (i > 0 && j > 0)
            	{
             		mat[i][j] = mat[i - 1][j - 1] + nr;
                    it[i][j] = i * n + j;
           
                }
            	else
            	{
               		mat[i][j] = nr;
                    it[i][j] = i * n + j;
  	            }
            }
            if (i > 0)
            {
              	if (cmp(i - 1,j, i, j))
               	{
                   	mat[i][j] = mat[i - 1][j];
                   	it[i][j] = it[i - 1][j];
               	}
            }
            if (j > 0)
            {
               	if (cmp(i, j - 1, i, j))
               	{
               		mat[i][j] = mat[i][j - 1];
                   	it[i][j] = it[i - 1][j];
               	}
            }
        }
    }
    aux1 = 0;
    aux2 = 0;
    for(i = 0;i < n; ++i)
    {
    	for(j = 0;j < n; ++j)
        {
    		if (cmp(i,j,aux1,aux2))
        	{
                aux1 = i;
                aux2 = j;

        	}
		}
    }
    int l1 = 0;
    int l2 = 0;
    i = aux1;
    j = aux2;
    while(it[i][j] != -1)
    {
        if (a[i] == a[n - j - 1])
        {
             	++l2;
               	st1[l2] = i;
                if (i != n -j - 1)
                {
                	++l1;
                	st2[l1] = n - j - 1;
                }
        		i--;
                j--;
        }
        if (i < 0) break;
        if (j < 0) break;
        if (a[i] != a[n - j - 1])
        {
        	i = it[i][j] / n;
        	j = it[i][j] % n;
    	}
    }

    for(i = l2;i > 0; --i)
    {
       printf("%c",a[st1[i]]);
    }
    for(i = 1;i <= l1; ++i)
    {
    	printf("%c",a[st2[i]]);
    }
    printf("\n");


    return 0;
    
}