UVA10062 Tell me the frequencies!【字符统计+sort】

Given a line of text you will have to find out the frequencies of the ASCII characters present in it. Thegiven 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. Maximumlength of each line is 1000.

Output

Print the ASCII value of the ASCII characters which are present and their frequency according to thegiven format below. A blank line should separate each set of output. Print the ASCII characters in theascending order of their frequencies. If two characters are present the same time print the informationof 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

 

 

问题链接UVA10062 Tell me the frequencies!

问题简述:(略)

问题分析:这是一个字符统计问题,需要排序后输出结果。

程序说明:需要注意输出格式,对于输入的行,输出结果之间隔一行。

题记:(略)

参考链接:(略)

 

AC的C++语言程序(重载比较运算符)如下:

/* UVA10062 Tell me the frequencies! */

#include <iostream>
#include <algorithm>
#include <stdio.h>

using namespace std;

struct Node {
    int letter;
    int cnt;
    friend bool operator <(const Node &a,const Node &b) {
        return a.cnt != b.cnt ? a.cnt < b.cnt : a.letter > b.letter;
    }
};

const int START = 32;
const int END = 128;
Node lcnt[END];

void init()
{
    for(int i=START; i<END; i++) {
        lcnt[i].letter = i;
        lcnt[i].cnt = 0;
    }
}

int main()
{
    int line = 0;
    string s;
    while(getline(cin, s)) {
        if(line++)
            printf("\n");

        init();

        for(int i=0; s[i]; i++)
            lcnt[(int)s[i]].cnt++;

        sort(lcnt + START, lcnt + END);


        for(int i=START; i<END; i++)
            if(lcnt[i].cnt > 0)
                printf("%d %d\n", lcnt[i].letter, lcnt[i].cnt);
    }

    return 0;
}

 

AC的C++语言程序(代入比较函数)如下:

/* UVA10062 Tell me the frequencies! */

#include <iostream>
#include <algorithm>
#include <stdio.h>

using namespace std;

typedef struct {
    int letter;
    int cnt;
} Node;

const int START = 32;
const int END = 128;
Node lcnt[END];

void init()
{
    for(int i=START; i<END; i++) {
        lcnt[i].letter = i;
        lcnt[i].cnt = 0;
    }
}

int cmp(Node a, Node b)
{
    return a.cnt != b.cnt ? a.cnt < b.cnt : a.letter > b.letter;
}

int main()
{
    int line = 0;
    string s;
    while(getline(cin, s)) {
        if(line++)
            printf("\n");

        init();

        for(int i=0; s[i]; i++)
            lcnt[(int)s[i]].cnt++;

        sort(lcnt + START, lcnt + END, cmp);


        for(int i=START; i<END; i++)
            if(lcnt[i].cnt > 0)
                printf("%d %d\n", lcnt[i].letter, lcnt[i].cnt);
    }

    return 0;
}

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值