Number of words
Problem descriptionlily's good friend xiaoou333 recently very empty, he thought of a meaningless thing, is to count the number of different words in an article. Your task below is to help xiaoou333 solve the problem.
Input has multiple sets of data, each set of rows, each of which is a small article. Each small article is composed of lowercase letters and spaces, no punctuation, and when you encounter #, the input ends.
Output each group only outputs an integer, which represents the total number of different words in an article.
Sample Input
You are my friend#
Sample Output
4
The
first time you see this problem, think of the string processing directly. But will be troublesome, even time space will be very wasteful, this time, the advantage of STL came out, see the code will know:
#include <cstdio> #include <iostream> #include <cstring> #include <sstream> #include <set >using namespace Std;int Main () { string s; while (Getline (cin,s) &&s!= "#") { set<string>f; Istringstream sin (s); string SS; while (SIN>>SS) F.insert (ss);//Stream A string from S to the SS printf ("%d\n", F.size ()); } return 0;}
other practices may be written in such a short code.
There is also about istringstream,c string flow, in particular, you can refer to:Http://baike.baidu.com/linkurl=Yn7V8OHotgDUuG_SWhUbhj7CiBw8dnHpTNMmsDn0PFXhkrH_SpGtHDon6V8MArHQFHYVxx9oZJYm3730dtOkcq
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 2072. Number of words "the advantages of STL and the use of string streams" "August 4"