Cod sursa(job #140308)

Utilizator testelatugelu testelatu testelatu Data 21 februarie 2008 18:39:22
Problema Ordine Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include<stdio.h>
#define alfa 27
int a[alfa];

int main()
{
 freopen("ordine.in","r",stdin);
 freopen("ordine.out","w",stdout);
 
 
 int n=0,aux1,max=0,i,aux2,pot,alt,prev,j;
 char a1;
 
 while( scanf("%c",&a1)!=EOF ){
        
 if( a1 !='\n' ){
aux1=int(a1)-96;
 ++a[ aux1 ];
 if( a[aux1] > max){
 max=a[aux1]; aux2=aux1;}
 ++n;}
} 
 
  if( max == (n /2) +1 ){
      
      for(i=1; i<=n; ++i)
       if( a[i] && i!=aux2 )
       printf("%c%c",char(a[i]+96),char(a[aux2]+96));
       printf("\n");
       }
  else{
  prev=0;
  pot=0;
      for(i=1; i<=n; ++i){
       
       pot=alfa;
       alt=0;
       
       for(j=1; j<=alfa; ++j){ 
         if( a[j] && j != prev && j <= pot )
         pot=j;
         if( a[j] && a[j] == (n-i+1)/2+1 )
         alt=j;
         }      
       
       if( alt ){
       printf("%c",char(alt+96));
       --a[alt];
       prev=alt;
       }
       else{
       printf("%c",char(pot+96));
       --a[pot];
       prev=pot;
       }
} 
        printf("\n");   
      
}
    
    return 0;
}