2010年9月17日 星期五

ACM 10062 - Tell me the frequencies!

#include <stdio.h>
#include <ctype.h>

struct Word
{
char ch;
int count;
};
char str[1002];
struct Word w[300], c;
int index = 0, repeatIndex ;

int check(char ch)
{
for (repeatIndex = 0; repeatIndex < index; repeatIndex ++)
{
if (w[repeatIndex].ch == ch)
return 0;
}
return 1;
}

void insert(char ch)
{
int i;
if (check(ch))
{
w[index].count = 1, w[index].ch = ch;
index ++;
for (i = index - 1; i >= 1; i --)
if (w[i].count < w[i - 1].count ||
w[i].count == w[i - 1].count && w[i].ch > w[i - 1].ch)
c = w[i], w[i] = w[i - 1], w[i - 1] = c;
else break;
}
else
{
w[repeatIndex].count ++;
for (i = repeatIndex; i < index - 1; i ++)
if (w[i].count > w[i + 1].count ||
w[i].count == w[i + 1].count && w[i].ch < w[i + 1].ch)
c = w[i], w[i] = w[i + 1], w[i + 1] = c;
else break;
}
}

void print()
{
int i;
for (i = 0; i < index; i ++)
printf("%d %d\n", w[i].ch, w[i].count);
}

int main()
{
int n, i, caseNum = 0;
while (gets(str))
{
if (caseNum ++ != 0) printf("\n");
for (i = 0; str[i]; i++)
insert(str[i]);
print();
index = 0;

}
return 0;
}


回目錄
回首頁

沒有留言 :

張貼留言

Related Posts Plugin for WordPress, Blogger...