HDU 2072(单词数)题解

以防万一,题目原文和链接均附在文末。那么先是题目分析:

【一句话题意】

“就是统计一篇文章里不同单词的总数”(已经是一句话了。。)

【题目分析】

明显需要去重,上set,因为按行分析,又没有EOLN用,于是上istringstream。

【算法流程】

读一行塞一行干一行爱一行。。。。。
发这篇的目的其实是备忘istringstream的用法的。这道题没难点。

 1 #include <iostream> 
 2 #include <sstream>
 3 #include <string>
 4 #include <algorithm>
 5 #include <set>
 6 #include <stdio.h>
 7 #include <stdlib.h>
 8 #include <string.h>
 9 #include <math.h>
10 
11 #define each(i,n) (int i=1;i<=(n);++i)
12 
13 using namespace std;
14 
15 int main() {
16     
17     set<string> wordList;
18     string word, line;
19     
20     while(getline(cin, line)) {  
21         if (line == "#") break;
22         istringstream stream(line);  
23         wordList.clear();
24         while(stream>>word) {
25             wordList.insert(word);
26         }
27         cout<<wordList.size()<<endl;
28     }     
29     return 0;  
30 
31 } 
32 /*
33 TestData(IN)
34 you are my friend
35 #
36 TestData(OUT)
37 4
38 */

题目链接:单词数(HDU 2072)

题目属性:语言练习题

相关题目:2095(随便找了一个写这里了= =)

题目原文:
【Desc】lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
【In】有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
【Out】每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
【SampIn/Out】参见代码下方的注释。

原文地址:https://www.cnblogs.com/blumia/p/hdu2072.html