URI Online Judge | 1251
Tell me the Frequencies!
By Shahriar Manzoor, Bangladesh
Timelimit: 1
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 characters.
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 | Sample Output |
AAABBC 122333 | 67 1 66 2 65 3 49 1 50 2 51 3 |
URI onlin judge solution 1251 | Tell me the frequencies in C++
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ZERO(x) memset(x, 0, sizeof(x))
#define FOR(i, n) for(int i = 0; i < (n); ++i)
using
namespace
std;
struct
Ch{
int
l, f; };
Ch arr[200];
bool
cmp(Ch a, Ch b)
{
if
(a.f == b.f)
return
(a.l > b.l);
return
(a.f < b.f);
}
int
main(
int
argc,
char
const
*argv[])
{
int
sz;
bool
b =
false
;
string s;
while
(getline(cin, s))
{
if
(b) cout <<
'\n'
;
b =
true
;
sz = s.length();
ZERO(arr);
FOR(i, 200)
arr[i].l = i;
FOR(i, sz)
arr[s[i]].f++;
stable_sort(arr, arr + 200, cmp);
FOR(i, 200)
if
(arr[i].f != 0)
cout << arr[i].l <<
" "
<< arr[i].f <<
'\n'
;
}
return
0;
}
No comments:
Write commentsTo know more about the problem, give us your valuable commment. We'll try to help you. Thanks