以防萬一,題目原文和鏈接均附在文末。那麼先是題目分析:
“就是統計一篇文章裡不同單詞的總數”(已經是一句話了。。)
明顯需要去重,上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】參見代碼下方的注釋。