Word Count (SET)

Source: Internet
Author: User
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.