UVa 10008 - What's Cryptanalysis?

10008 - What's Cryptanalysis?

基本題,第一行的數字告訴你接下來有幾行輸入,讀完輸入後算各字母出現(不分大小寫)次數,然後照出現次數由高到低排序,同數量就以A-Z順序排。

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);

using namespace std;

class alphabet{
	public:
		alphabet(int i)
		{
			upper='A'+i;
			count=0;
		}
	
		//for greater sort 
		bool operator>(const alphabet& rValue)const
		{
			return (count > rValue.count) || ((count == rValue.count) && (upper<rValue.upper));
		}
		
		void print()
		{
			cout << upper << ' ' << count << endl;
		}
		
		char upper;
		int count;
};

int main(int argc, char* argv[])
{
	vector<alphabet> alphArr;
	int i=0;
	
	while(i<26)
		alphArr.push_back(alphabet(i++));
	
	int lineCount=0;
	string str;
	
	cin >> lineCount;
	//lineCount++;
	char c;
	while (cin.get(c) && c != '\n');
	
	while(lineCount--)
	{
		getline(cin, str);

		for(string::iterator str_i=str.begin(); str_i!=str.end(); ++str_i)
		{	
			if((((*str_i)>='a')&&((*str_i)<='z')))
				alphArr.at((*str_i)-'a').count++;
			else if((((*str_i)>='A')&&((*str_i)<='Z')))
				alphArr.at((*str_i)-'A').count++;
		}	
	}
	
	sort(alphArr.begin(), alphArr.end(), greater<alphabet>());
	
	vector<alphabet>::iterator alph_ite=alphArr.begin();
	
	while((*alph_ite).count)
	{
		(*alph_ite).print();
		alph_ite++;
	}
	
	return 0;
}

Related Article