Lily's good friend xiaoou333 was blank recently. He thought of a meaningless thing, that is, to count the total number of different words in an article. The following task helps xiaoou333 solve this problem. Input contains multiple groups of data, one row in each group, and each group is a small article. Each small article is composed of lower-case letters and spaces with no punctuation marks. If it encounters "#", the input ends. Each group outputs only one integer, which is a separate line. This integer represents the total number of different words in an article. Sample Input
you are my friend#
Sample output
4
Idea: The dictionary tree can still solve this problem, but recently I learned STL, so I used set to solve this problem.
Set can be understood as a set of mathematics, so ask your high school teacher about the concept of set.
Direct AC code:
#include<iostream>#include<string>#include<set>#include<sstream>using namespace std;set <string> dict;int main(){ string s,buff; while(getline(cin,s)) { dict.clear(); int tot=0; if(s[0]=='#') break; for(int i=0;i<s.length();i++) if(isalpha(s[i])) s[i]=tolower(s[i]); else s[i]=' '; stringstream ss(s); while(ss>>buff) { dict.insert(buff); } for(set<string>::iterator it=dict.begin();it!=dict.end();++it) { tot++; } cout<<tot<<endl; } return 0;}
Word Count (SET)