单词数
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 21651 Accepted Submission(s): 5228
Problem Description
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
Sample Input
you are my friend
#
Sample Output
4
Author
Lily
Source
Recommend
思路: 先设置一个字符型数组来存储字符,然后逐一将每一个单词存到二维数组中。接着排序,然后统计不同即可!!
刚开始数组开小了,runtime error很多次!!最后改到很大才过了,比较的那个....
估计用c++会好很多.....


1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #define maxn 10001 5 char paper[maxn][100]={'\0'}; 6 char str[maxn]={'\0'}; 7 int cmp(const void *a,const void *b) 8 { 9 return strcmp((char *)a,(char *)b); 10 } 11 int main() 12 { 13 14 int i,count,k; 15 bool flag; 16 while(gets(str),*str!='#') 17 { 18 flag=true; 19 count=-1; 20 memset(paper,'\0',sizeof(paper)); 21 for( i=0 ; str[i]!='\0' ; i++ ) 22 { 23 if(flag&&str[i]!=' ') 24 { 25 flag=false; 26 count++; 27 k=0; 28 } 29 else 30 if(flag==false&&str[i]==' ') 31 { 32 flag=true; 33 continue; 34 } 35 if(str[i]!=' ') 36 { 37 paper[count][k++]=str[i]; 38 } 39 40 } 41 memset(str,'\0',sizeof(str)); 42 //for(i=0;i<=count;i++) 43 //puts(paper[i]); 44 if(count>0) 45 qsort(paper,count+1,100*sizeof(char),cmp); 46 /* for(i=0;i<=count;i++) 47 puts(paper[i]); 48 */ 49 int sum=1; 50 for(i=1;i<=count;i++) 51 { 52 if(strcmp(paper[i-1],paper[i])!=0) 53 sum++; 54 } 55 if(count==-1)sum=0; 56 printf("%d\n",sum); 57 58 } 59 return 0; 60 }