单词数 2072

Problem Description
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
 
Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
 
Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
 
Sample Input
you are my friend #
 
Sample Output
4
 
Author
Lily
 
Source
 
Recommend
linle   |   We have carefully selected several similar problems for you:  1251 1020 1075 1247 2069 
 
 
 
 
 
 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<string.h>
 4 #include<algorithm>
 5 #include<string>
 6 using namespace std;
 7 int main()
 8 {
 9     char str[10010];
10     string    b[10010];
11     int i;
12     while(gets(str)&&str[0]!='#')
13     {
14     //    cout<<str<<endl;
15         char *p=strtok(str," ");
16         int k=0,count=0;
17         while(p)
18         {
19             b[k++]=p;
20             p=strtok(NULL," ");
21         }
22         sort(b,b+k);
23     
24         for( i=0;i<k;i++)
25         {
26             while(i<k-1&&b[i]==b[i+1])//aabbd
27             {
28                 i++;
29             }
30             count++;
31         }
32         printf("%d
",count);
33     }
34     return 0;
35 }

要好好理解sort函数。

原文地址:https://www.cnblogs.com/wangmengmeng/p/4626816.html