單詞數 (STL set集合)

來源:互聯網
上載者:User

標籤:des   style   color   os   io   資料   ar   問題   

單詞數Problem Descriptionlily的好朋友xiaoou333最近很空,他想了一件沒有什麼意義的事情,就是統計一篇文章裡不同單詞的總數。下面你的任務是協助xiaoou333解決這個問題。 Input有多組資料,每組一行,每組就是一篇小文章。每篇小文章都是由小寫字母和空格組成,沒有標點符號,遇到#時表示輸入結束。 Output每組只輸出一個整數,其單獨成行,該整數代表一篇文章裡不同單詞的總數。 Sample Input
you are my friend#
 Sample Output
4set集合用法及特點:                1,set中的元素從小到大排列且和集合一樣沒有重複元素;         2,set<string>::iterator     s.begin()   s.end()
#include<iostream># include<cstring>#include<cstdio># include<sstream># include<string>#include<set>using namespace std; string wen,str; set<string> dict;int main(){    while(getline(cin,wen)&&wen!="#")    {        stringstream  ch(wen);                      //在文本串中找單個單詞        while(ch >>str)            dict.insert(str);          printf("%d\n",dict.size());          dict.clear();    }    return 0;}


     和用map差不多:
 #include<iostream> # include<cstdio> # include<string> # include<map> # include<cstring> # include<sstream> using namespace std; map<string ,int> m; string str,ch;/* int main() {     while(getline(cin,str)&&str!="#")     {         int len=str.size();         int i=0;         m.clear();          while(i<len)         {             ch="";             while(i<len&&'a'<=str[i]&&str[i]<='z')                    ch+=str[i++];              if(ch!="")             {                m[ch]=1;             }             i++;         }         printf("%d\n",m.size());     }     return 0; }*/int main(){    while(getline(cin,str)&&str!="#")    {       stringstream ss(str);       m.clear();       while(ss>>ch)       {          m[ch]=1;       }       printf("%d\n",m.size());    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.