Mai intai trebuie sa te autentifici.
Cod sursa(job #155791)
Utilizator | Data | 12 martie 2008 10:17:01 | |
---|---|---|---|
Problema | Ordine | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva de probleme | Marime | 0.8 kb |
# include <stdio.h>
# include <string.h>
const long int MAXN=1000000;
long int count[100],n;
void citire()
{
char s[MAXN+10];
FILE *f=fopen("ordine.in","r");
fgets(s,MAXN,f);
fcloseall();
long int i=strlen(s)-2;
n=i+1;
while (i>=0)
{
count[(int)s[i]-(int)'a'+1]++;
i--;
}
}
void scrie()
{
FILE *g=fopen("ordine.out","w");
long int old=0,j,i,ok;
while (n)
{
ok=0;
for (j=1;j<=(int)'z'-(int)'a'+1;j++) if (count[j]==n/2+1)
{
fprintf(g,"%c",(char)((int)'a'+j-1));
count[j]--;
n--;
old=j;
ok=1;
break;
}
if (ok==0)
{
//if (!old)
{
i=1;
while (count[i]==0||i==old) i++;
}
old=i;
count[i]--;
fprintf(g,"%c",(char)((int)'a'+i-1));
n--;
}
}
fprintf(g,"\n");
fcloseall();
}
int main()
{
citire();
scrie();
return 0;
}