UVa 10062 - Tell me the frequencies!

''' Store the element and count in a structure called node. Create an array of nodes. Then count the frequencies. Now for count > 1, sort according to the criteria smaller counts first, if counts are equal then higher element (element of higher ASCII value ) first. This is an inefficient sort but it is only approx 300 *300 = 9*10^4. I was getting an error when i used while(scanf(" %s",s)!=-1). Note that space before %s. Better to use fgets .Other things are easy.

- temper3243@gmail.com

'''

/* temper3243@gmail.com
 * 1) include
 * 1) include

typedef struct node { int ele; int count; }node;

node arr[300],tmp,tarr[300];

int main {	char s[1009]; int i,len=0,j,cnt=0,mycnt=0;

while(fgets(s,1003,stdin)!=NULL) {		if(mycnt>0) printf("\n"); len=strlen(s); for(i=0;i<300;i++) arr[i].ele=arr[i].count=0;

for(i=0;i 0) {			tarr[cnt++]=arr[i]; }

for(i=0;i tarr[j].count) {				tmp=tarr[i]; tarr[i]=tarr[j]; tarr[j]=tmp; }			else if(tarr[i].count ==tarr[j].count) {				if(tarr[i].ele < tarr[j].ele) {				tmp=tarr[i]; tarr[i]=tarr[j]; tarr[j]=tmp; }			}		}

for(i=0;i<cnt;i++) printf("%d %d\n",tarr[i].ele,tarr[i].count);

mycnt++; }	return 0; }