HDOJ, 杭电1219, ACme简单字符串题

字符串系列题目,明天继续刷,将要用到字典树。

需要先学习一下。


水题代码如下:

/***** HDOJ 1219 ACme简单字符串题 ********/

/******** written by C_Shit_Hu ************/

////////////////简单字符串题目///////////////

/****************************************************************************/
/* 
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
*/
/****************************************************************************/


// 比较水,代码如下。

#include <stdio.h>
#include <string.h>
#define N 100001

// 主函数
int main()
{
	char s[N];        // 存储输入的“文章”
	int letter[26],i;      // 存储相应的字符出现的次数,临时变量

	while(gets(s))    // 核心循环
	{
	    memset(letter, '\0', sizeof(letter)) ;     
		for(i=0; s[i]!='\0'; i++)
			if(s[i]>='a' && s[i]<='z') 
				letter[ s[i]-'a' ]++;   // 这个for循环为核心代码
		for(i=0;i<26;i++)
			printf("%c:%d\n",'a'+i, letter[i]);
			printf("\n");
	}
	return 0;
}

/******************************************************/
/********************  心得体会  **********************/
/*
果然是水题。。。
还做的那么慢。。

  水水更健康!!!
*/
/******************************************************/



你可能感兴趣的:(HDOJ, 杭电1219, ACme简单字符串题)