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;
}