(hdu 简单题 128道)AC Me(统计一行文本中各个字母出现的次数)

题目:

AC Me

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13465    Accepted Submission(s): 5927

Problem Description

Ignatius is doing his homework now. The teacher gives him some articles and asks him to tell how many times each letter appears.

It‘s really easy, isn‘t it? So come on and AC ME.

Input

Each article consists of just one line, and all the letters are in lowercase. You just have to count the number of each letter, so do not pay attention to other characters. The length of article is at most 100000. Process to the end of file.

Note: the problem has multi-cases, and you may use "while(gets(buf)){...}" to process to the end of file.

Output

For each article, you have to tell how many times each letter appears. The output format is like "X:N".

Output a blank line after each test case. More details in sample output.

Sample Input

hello, this is my first acm contest!
work hard for hdu acm.

Sample Output

a:1
b:0
c:2
d:0
e:2
f:1
g:0
h:2
i:3
j:0
k:0
l:2
m:2
n:1
o:2
p:0
q:0
r:1
s:4
t:4
u:0
v:0
w:0
x:0
y:1
z:0

a:2
b:0
c:1
d:2
e:0
f:1
g:0
h:2
i:0
j:0
k:1
l:0
m:1
n:0
o:2
p:0
q:0
r:3
s:0
t:0
u:1
v:0
w:1
x:0
y:0
z:0

Author

Ignatius.L

Source

杭州电子科技大学第三届程序设计大赛

Recommend

Ignatius.L   |   We have carefully selected several similar problems for you:  1200 1201 1220 1235 1234

题目分析:

简单题。统计各个字母出现的次数。

代码如下:

/*
 * a.cpp
 *
 *  Created on: 2015年3月11日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>
#include <map>
#include <cstring>

using namespace std;

int main() {
	string str;

	int cnt[26];//开一个数组,用于存储每个字符出现的次数
	while (getline(cin, str)) {//读入一行数据.不要用C++提交,可能会CE
		memset(cnt, 0, sizeof(cnt));

		int len = str.length();
		int i;
		for (i = 0; i < len; ++i) {//遍历整个字符串
			if(str[i] >= ‘a‘ && str[i] <= ‘z‘){//如果该字符是一个字母
				cnt[str[i] - ‘a‘]++;//统计该字母出现的次数
			}
		}

		for(i = 0 ; i < 26 ; ++i){//遍历cnt数组
			printf("%c:%d\n",‘a‘+i,cnt[i]);//输出各个字母出现的次数
		}

		printf("\n");
	}

	return 0;
}
时间: 2024-08-07 17:01:40

(hdu 简单题 128道)AC Me(统计一行文本中各个字母出现的次数)的相关文章

(hdu 简单题 128道)平方和与立方和(求一个区间的立方和和平方和)

题目: 平方和与立方和 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 108212    Accepted Submission(s): 34915 Problem Description 给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和. Input 输入数据包含多组测试实例,每组测试实例包含一行,由两个整数m和n组

(hdu 简单题 128道)Lowest Bit(求一个数的二进制的最后一个非零位对应的十进制数)

题目: Lowest Bit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8722    Accepted Submission(s): 6428 Problem Description Given an positive integer A (1 <= A <= 100), output the lowest bit of A.

(hdu 简单题 128道)hdu 1194 Beat the Spread!(已知两个数的和u两个数的差求这两个数)

题目: Beat the Spread! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5169    Accepted Submission(s): 2692 Problem Description Superbowl Sunday is nearly here. In order to pass the time waiting f

(hdu 简单题 128题)hdu 2005 第几天(计算当天是该年的第几天)

题目: 第几天? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 89641    Accepted Submission(s): 33727 Problem Description 给定一个日期,输出这个日期是该年的第几天. Input 输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,

循环-06. 统计一行文本的单词个数

循环-06. 统计一行文本的单词个数(15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 张彤彧(浙江大学) 本题目要求编写程序统计一行字符中单词的个数.所谓“单词”是指连续不含空格的字符串,各单词之间用空格分隔,空格数可以是多个. 输入格式: 输入给出一行字符. 输出格式: 在一行中输出单词个数. 输入样例: Let's go to room 209. 输出样例: 5 1 #include<stdio.h> 2 #incl

黑马程序员——统计一个字符串中各个字符出现的次数

统计一个字符串中各个字符出现的次数 import java.util.Iterator; import java.util.Set; import java.util.TreeMap; public class TreeMapDemo { //统计一个字符串中相应字符出现的次数 public static void main(String[] args) { // String s = "aagfagdlkerjgavpofjmvglk我是你的"; //调用自定义方法来 统计相应字符出

循环-06. 统计一行文本的单词个数(15)

1 #include<iostream> 2 #include<string> 3 using namespace std; 4 int main(){ 5 string s; 6 int i,c=0; 7 getline(cin,s); 8 for(i=1;i<s.length()-1;i++) 9 if(s[i]==' '&&s[i-1]!=' ') 10 c++; 11 if(s[i]!=' ') 12 c++; 13 cout<<c<

Java编程练习之判断Java文件名是否正确,判断邮箱格式是否正确和统计指定字符串中某字符现的次数

一判断Java文件名是否正确,判断邮箱格式是否正确 功能:判断Java文件名是否正确,判断邮箱格式是否正确.其中:合法的文件名应该以.java结尾:合法的邮箱名 中至少要包含 "@" , 并要求 "@" 在 "." 之前. 练习代码: public class Test { public static void main(String[] args) { //Java文件名 String fileName = "HelloWorld.j

统计一个文件中出现字符&#39;a&#39;的次数

# -*- coding: utf-8 -*- #python 27 #xiaodeng #统计一个文件中出现字符'a'的次数 #http://www.cnblogs.com/hongten/p/hongten_python_count.html import os number=0 def getNumber(filePath,c): 'c---->the word numbers' #统计一个文件中出现字符'a'的次数 if os.path.exists(filePath): global