填寫問券˙抽7-11禮券來自星星的你也會打廣告?這畫面也太叫人垂涎欲滴!反服貿流血 法官要警交...
2012-05-12 08:09:54 人氣(45) | 回應(0) | 推薦(0) | 收藏(0) 上一篇 | 下一篇

[UVA][String] 10062 - Tell me the frequencies!

0
收藏
0
推薦

Problem H

Tell me the frequencies!

Input: standard input

Output: standard output

 

Given a line of text you will have to find out the frequencies of the ASCII characters present in it. The given lines will contain none of the first 32 or last 128 ASCII characters. Of course lines may end with ‘\n’ and ‘\r’ but always keep those out of consideration.


Input

Several lines of text are given as input. Each line of text is considered as a single input. Maximum length of each line is 1000.


Output

Print the ASCII value of the ASCII characters which are present and their frequency according to the given format below. A blank line should separate each set of output. Print the ASCII characters in the ascending order of their frequencies. If two characters are present the same time print the information of the ASCII character with higher ASCII value first. Input is terminated by end of file.


Sample Input:

AAABBC
122333

Sample Output:

67 1
66 2
65 3

49 1
50 2
51 3



#include <stdio.h>
#include <stdlib.h>

struct node {
    int v, c;
    node() {
        v = 0;
        c = 0;
    }
};
int cmp(const void *i, const void *j) {
    node *a, *b;
    a = (node *)i, b = (node *)j;
    if(a->v != b->v)
        return a->v - b->v;
    return b->c - a->c;
}
int main() {
    int i, j, flag = 0;
    char str[1005];
    while(gets(str)) {
        node I[128];
        for(i = 0; i < 128; i++)
            I[i].c = i;
        for(i = 0; str[i]; i++)
            I[str[i]].v++;
        for(i = 0, j = 0; i < 128; i++) {
            if(I[i].v) {
                I[j++] = I[i];
            }
        }
        if(flag++)
            puts("");
        qsort(I, j, sizeof(node), cmp);
        for(i = 0; i < j; i++)
            printf("%d %d\n", I[i].c, I[i].v);
    }
    return 0;
}

10062Tell me the frequencies!
台長:Morris
人氣(45) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][ConvexHull] 109 - SCUD Busters
此分類上一篇:[UVA][String] 468 - Key to Success

我要回應
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入以下數字 (ex:123)

(有*為必填)
詳全文