#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct name_value
{
int ascii;
int count;
};
int main()
{
string s;
bool endl_flag = false;
while(getline(cin, s))
{
if(endl_flag) cout << endl;
vector list;
for(int i=0;i<s.size();++i)
{
bool flag = false;
for(int j=0;j<list.size();++j)
{
if((int)s[i] == list[j].ascii)
{
list[j].count++;
flag = true;
break;
}
}
if(!flag) // 新增一筆到 list
{
struct name_value a;
a.ascii = (int)s[i];
a.count = 1;
list.push_back(a);
}
}
//Sort by frequency (Head is the smallest number)
for(int i=list.size() - 1;i>0;--i)
{
for(int j=0;j<i;++j)
{
if(list[j].count > list[j+1].count)
swap(list[j], list[j+1]);
}
}
//Sort by ascii (Head is the smallest number)
for(int i=list.size() - 1;i>0;--i)
{
for(int j=0;j<i;++j)
{
if(list[j].count == list[j+1].count && list[j].ascii < list[j+1].ascii)
swap(list[j], list[j+1]);
}
}
//output
for(int i=0;i<list.size();++i)
{
cout << list[i].ascii << " " << list[i].count << endl;
}
endl_flag = true; // 最後不要印出換行
}
return 0;
}
文章標籤
全站熱搜