#include <iostream>
#include<string>
#include<queue>
using namespace std;
struct CharCount
{
char c;
int freq;
bool operator< (const CharCount &rop) const
{
return freq < rop.freq;
}
};
/*
* [A, Z] ===> [65, 90]
* [a, z] ===> [97, 122]
*
*/
int char_count(void)
{
int count[52] = {0};
string str;
while (cin >> str)
{
char c;
memset(count, 0, sizeof count);
int len = str.length();
for (int i = 0; i < len; i++)
{
c = str[i];
if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122)) /* check the char */
{
c - 'A' > 25 ? count[26+c-'a']++ : count[c-'A']++;
}
}
priority_queue<CharCount> que ;
while (!que.empty())
que.pop();
for (i =0; i < 52; i++)
{
/*
cout << char (i > 25 ? i - 26 + 97 : i + 65) << " " << count[i] << "\t";
if (count[i])
cout << char (i > 25 ? i - 26 + 97 : i + 65) << ": " << count[i] << "\t";
*/
CharCount temp;
temp.freq = count[i];
temp.c = char (i > 25 ? i - 26 + 97 : i + 65);
que.push(temp);
}
cout << que.top().c << ": " << que.top().freq << "\n";
}
return 0;
}
int main()
{
return char_count();
}
当多个字符频率一致时候,具体显示哪一个字符应该是由c++中的priority_queue的实现决定的。
和上一个版本相比基本思想没有变化。