word count
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 62366 Accepted Submission(s): 15500
Problem Description
Lily's good friend xiaoou333 has been very busy recently. He thought about a meaningless thing, which is to count the total number of different words in an article. Your task below is to help xiaoou333 solve this problem.
Input
There are multiple sets of data, each set is a row, and each set is a small article. Each small article is composed of lowercase letters and spaces, no punctuation marks, and the end of input when # is encountered.
Output
Each group outputs only one integer, on its own line, that represents the total number of distinct words in an article.
Sample Input
you are my friend#
Sample Output
4
Author
#include<iostream> #include<string> #include<set> using namespace std; intmain() { string s,s1; set<string>set1; while(getline(cin,s)) { if(s[0]=='#') break; for(int i=0;i<=s.size()-1;i++) { if(s[i]!=' ') s1=s1+s[i]; if((s[i]==' '&&!s1.empty())||(i==s.size()-1&&!s1.empty())) { set1.insert(s1); s1.clear(); } } cout<<set1.size()<<endl; set1.clear(); } return 0; }