-
Notifications
You must be signed in to change notification settings - Fork 0
/
10062 - Tell me the frequencies!.cpp
85 lines (69 loc) · 1.95 KB
/
10062 - Tell me the frequencies!.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<cstdio>
#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
int main(void)
{
int k = 0;
string str;
while(getline(cin,str))
{
vector<char>symbols;
vector<int>freq;
for(int i = 0; i < str.length(); i++)
{
symbols.push_back(str[i]);
}
int limit = symbols.size();
for(int i = 0; i < limit; i++)
{
int count = 1;
for(int j = i+1; j < limit; j++)
{
if(symbols[i] == symbols[j])
{
count++;
symbols.erase(symbols.begin()+j);
j--;
limit = symbols.size();
}
limit = symbols.size();
}
limit = symbols.size();
freq.push_back(count);
}
for(int i = 0; i < symbols.size(); ++i)
{
for(int j = 0; j < symbols.size() - i - 1; ++j)
{
if(freq[j+1] < freq[j])
{
int temp = freq[j];
freq[j] = freq[j+1];
freq[j+1] = temp;
char tempC = symbols[j];
symbols[j] = symbols[j+1];
symbols[j+1] = tempC;
}
else if(freq[j+1] == freq[j])
{
if(symbols[j+1] > symbols[j])
{
char tempC = symbols[j];
symbols[j] = symbols[j+1];
symbols[j+1] = tempC;
}
}
}
}
if(k != 0) cout << endl;
for(int i = 0; i < symbols.size(); ++i)
{
cout << (int)symbols[i] << " " << freq[i] << endl;
}
k++;
}
return 0;
}