單詞數
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 54686 Accepted Submission(s): 13604
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: 2074 2054 2052 1251 2058
這道題我想了兩種方法,這裏只寫出最簡單的
代碼如下
#include <iostream>
#include <set>
#include <string>
#include <sstream> //使用stringstream的時候需要的頭文件
using namespace std;
int main() {
string s;
while(getline(cin,s) && s!= "#")
{
istringstream str(s);
string word;
set<string> a;
while(str>>word)//傳值
{
a.insert(word);
}
cout <<a.size() <<endl;
}
return 0;
}