题目传送门:
http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=1§ionid=2&problemid=26
由于输入全部都是小写字母a-z(0x61H-0x61+25),因此我们使用数组的时候有个技巧,可以将其减去0x61H映射到0-25的集合中去,而这个集合就作为times(出现次数)数组,剩下的就是输入输出问题了。
#include<stdio.h>
#include<string.h>
using namespace std;
//HDU AC Me
int time[26]={0};
int main(){
//freopen("input.txt","r",stdin);
char buffer[1000000]; //数组开大一点,否则会出错,注意题目要求
//gets可以读空格,但是scanf不可以
while(gets(buffer)!=0){
int len=strlen(buffer);
for(int i=0;i<len;i++){
if(buffer[i]>=0x61 &&buffer[i]<=0x61+25)
time[buffer[i]-0x61]++;
}
for(int j=0;j<26;j++){
printf("%c:%d\n",0x61+j,time[j]);
}
printf("\n");
memset(time,0,sizeof(time));
}
return 0;
}