Cod sursa(job #37584)

Utilizator andytrAlexandru Traista andytr Data 25 martie 2007 11:13:17
Problema Elimin 2 Scor 0
Compilator cpp Status done
Runda preONI 2007, Runda 4, Clasa a 10-a Marime 0.83 kb
#include <stdio.h>
#include <string.h>
#define Nmax 2002

char s[Nmax],b[Nmax][Nmax];
int a[2][2],l;
FILE *f;

void cit()
{f=fopen("elimin2.in","r");
int i=0;
while(!feof(f)&&fscanf(f,"%c",&s[i++]))
 ;
fclose(f);
}

void sol()
{int i,j;
l=strlen(s);
for(i=1;i<=l;i++)
 for(j=1;j<=l;j++)
  if(s[i-1]==s[l-j])
    {a[i%2][j%2]=a[(i+1)%2][(j+1)%2]+1;
     b[i][j]='=';
    }
   else
    if(a[(i-1)%2][j%2]>a[i%2][(j+1)%2])
      {a[i%2][j%2]=a[(i+1)%2][j%2];
       b[i][j]='>';
      }
     else
      {a[i%2][j%2]=a[i%2][(j+1)%2];
       b[i][j]='<';
      }
}

void scr(int x,int y)
{if(b[x][y]=='=')
  {scr(x-1,y-1);
   fprintf(f,"%c",s[x-1]);
  }
if(b[x][y]=='>')
 scr(x-1,y);
if(b[x][y]=='<')
 scr(x,y-1);
}

int main()
{cit();
sol();
f=fopen("elimin2.out","w");
scr(l,l);
fclose(f);
return 0;
}