(字符串的模式匹配4.7.8)UVA 10008 What's Cryptanalysis?(统计文本中某一个字符的出现的次数,需从小到大排序&&同次数的按字段序排序)

(字符串的模式匹配4.7.8)UVA 10008 What's Cryptanalysis?(统计文本中某一个字符的出现的次数,需要从小到大排序&&同次数的按字段序排序)

/*
 * UVA_10008.cpp
 *
 *  Created on: 2013年10月26日
 *      Author: Administrator
 */

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>


using namespace std;

struct inf{
	int num, id;
}a[30];

bool cmp(inf x, inf y) {
	if(x.num != y.num) return x.num > y.num;
	return x.id < y.id;
}


int main(){
	int n;
	char s[1000];
	while(scanf("%d",&n)!=EOF){
		int i,j;
		for(i = 0 ; i < 26 ; ++i){
			a[i].id = i;
			a[i].num = 0;
		}

		gets(s);
//		scanf("%s",&s);
		for(i = 0 ; i < n ; ++i){

//			scanf("%s",&s);
			gets(s);
			for(j = 0 ; j < strlen(s);++j){
				if(isalpha(s[j])){//判断该字符是否是字母,排除空格符及?号等情况
					a[tolower(s[j]) - 'a'].num++;//tolower(s[j])将所有字符都穿换成小写
				}
			}
		}

		sort(a,a+26,cmp);

		for(i = 0 ; i < 26 ; ++i){
			if(a[i].num !=0){

				printf("%c %d\n",(a[i].id +'A'),a[i].num);
			}
		}
	}

	return 0;
}