题目描述
source: http://acm.hdu.edu.cn/showproblem.php?pid=2072
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 45580 Accepted Submission(s): 11194
Problem Description
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
Sample Input
you are my friend
#
Sample Output
4
Author
Lily
Source
浙江工业大学网络选拔赛
#include <iostream>
#include <algorithm>
#include <cstring>
#include <set>
#include <string.h>
using namespace std;
int main(){
set<string> ss;
char s[1000];
char* token;
while((cin.getline(s, 1000)) && s[0] != '#'){
ss.clear();
token = strtok(s, " "); // 提取字符串空格
while(token != NULL){
ss.insert(token);
token = strtok(NULL, " ");
}
//show
cout << ss.size() << endl;
}
return 0;
}
//优势
// 使用set模板, 简化统计相同单词的个数过程
// strtok,自动delete字符串中的空格
//改进
// 虽然使用了STL, strtok函数,但是开销增加。
// set排序过程加大了开支。
jok: algorithm is kidding me
参考资料
getline: https://msdn.microsoft.com/zh-cn/library/2whx1zkx.aspx
set : https://msdn.microsoft.com/zh-cn/library/e8wh7665.aspx