Cod sursa(job #45411)

Utilizator pauldbPaul-Dan Baltescu pauldb Data 1 aprilie 2007 14:38:03
Problema Elimin 2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.9 kb
#include <stdio.h>
#include <string.h>

#define maxn 2010
#define maxc 10
#define maxv 10000

char a[maxn];
short c[maxn][maxn];
short left[maxn][maxc],right[maxn][maxc];
int n,sol,l;
char v[maxn];

int main()
{
    freopen("elimin2.in","r",stdin);
    freopen("elimin2.out","w",stdout);
    
    int i,j,x,y,cif,aux;
    fgets(a,maxn,stdin);
    n=strlen(a)-2;
    
    for (i=0;i<maxc;i++) left[n][i]=maxn;
    left[n][a[n]-'0']=n;
    for (i=0;i<maxc;i++) right[0][i]=-maxn;
    right[0][a[0]-'0']=0;
    
    for (i=n-1;i>=0;i--) 
      for (j=0;j<maxc;j++)
        if (j==a[i]-'0') left[i][j]=i;
        else left[i][j]=left[i+1][j];
        
    for (i=1;i<=n;i++)
      for (j=0;j<maxc;j++)
        if (j==a[i]-'0') right[i][j]=i;
        else right[i][j]=right[i-1][j];
        
	for (i=0;i<=n;i++) c[i][i]=1;
	for (i=0;i<n;i++)
	  if (a[i]==a[i+1]) c[i][i+1]=2;
	  else c[i][i+1]=0;
      
    for (i=0;i<=n;i++) 
      for (x=0;x+i<=n;x++) 
      {
          y=x+i;
          if ((x>0) && (y<n) && (a[x-1]==a[y+1]) && (c[x][y]+2>c[x-1][y+1])) c[x-1][y+1]=c[x][y]+2;
          if ((x>0) && (c[x][y]>c[x-1][y])) c[x-1][y]=c[x][y];
          if ((y<n) && (c[x][y]>c[x][y+1])) c[x][y+1]=c[x][y];
      }  
      
    for (i=maxc-1;i>0;i--)
      if (c[left[0][i]][right[n][i]]>sol) 
      {
         sol=c[left[0][i]][right[n][i]];
         x=left[0][i];
         y=right[n][i];
         cif=i;
      }            
      
    aux=sol-1;
    j=0;
    v[j++]=cif;
    v[aux]=cif;
    
	while (x+1<y)
      for (i=maxc-1;i>=0;i--)
        if (c[left[x+1][i]][right[y-1][i]]==sol-2)
        {
             x=left[x+1][i];
             y=right[y-1][i];
             sol-=2;
             v[j]=i;
             v[aux-j]=i;
             j++;
             break;   
        }
        
	for (i=0;i<=aux;i++) printf("%d",v[i]);
    printf("\n");
    
    return 0;
}