单词数
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 35863 Accepted Submission(s): 8662
Problem Description
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
Sample Input
you are my friend
#
Sample Output
4
Author
Lily
Source
Recommend
//做到差点心碎 ; AC代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 char str[1010], ch[110][110] , cmp[1010]; 5 using namespace std ; 6 7 void sweep(char a[], char b[]) 8 { 9 char c[1010] ; 10 strcpy(c, a); strcpy(a, b); strcpy(b, c) ; 11 } 12 int main() 13 { 14 15 while(gets(str), str[0] != '#') 16 { 17 memset(ch, '\0', sizeof(ch)) ; 18 int i, j, front=-1, rear=-1, time = 0 ; //标记 ; 19 int len = strlen (str) ; 20 // printf("%d\n", len) ; 21 int t = 0 ; 22 for(i=0; i<len; i++) 23 { 24 if(str[i] == ' ') 25 t++ ; 26 } 27 if(t == len || len == 0) //没有输入 || 输入全是空格 → 结果为 0 ; 28 { 29 printf("0\n") ; 30 continue ; 31 } 32 else 33 { 34 35 for(i=0; i<len; i++) 36 { 37 if(str[i] !=' ' && (str[i-1] == ' ' || i == 0)) 38 front = i ; 39 if(str[i] != ' '&& (str[i+1]==' ' || str[i+1] == '\0')) 40 rear = i ; 41 int ti = 0 ; 42 // printf("%d %d\n",front, rear) ; 43 if(rear != -1 && front != -1) //单词分离出来 ; 44 { 45 //printf("%d %d\n", front, rear) ; 46 for(i=front; i<=rear; i++ ) 47 { 48 ch[time][ti++] = str[i] ; 49 } 50 time++ ; rear = -1 ; front = -1 ; 51 } 52 } 53 for(i=0; i<time; i++) //遍历排序 ; 54 { 55 for(j=0; j < time ; j++) 56 { 57 if(strcmp(ch[i], ch[j]) > 0 ) 58 sweep(ch[i], ch[j]) ; 59 } 60 } 61 62 int total = 1 ; 63 for(i=1; i<time; i++)//判断不同单词数 ; 64 if(strcmp(ch[i], ch[i-1]) != 0) 65 total++ ; 66 printf("%d\n", total) ; 67 } 68 } 69 return 0 ; 70 }