1. 程式人生 > >【字串】HDU2072單詞數

【字串】HDU2072單詞數

<div class="panel_title" align="left" style="height: 38px; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background: url("panel-title.png") 0% 100% no-repeat transparent;">Problem Description</div><div class="panel_content" style="height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background: url("panel-content.png") repeat-y;">lily的好朋友xiaoou333最近很空,他想了一件沒有什麼意義的事情,就是統計一篇文章裡不同單詞的總數。下面你的任務是幫助xiaoou333解決這個問題。</div><div class="panel_bottom" style="height: auto; margin: 0px; font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center; background: url("panel-bottom.png") 0% 0% no-repeat;"> </div><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background: url("panel-title.png") 0% 100% no-repeat transparent;">Input</div><div class="panel_content" style="height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background: url("panel-content.png") repeat-y;">有多組資料,每組一行,每組就是一篇小文章。每篇小文章都是由小寫字母和空格組成,沒有標點符號,遇到#時表示輸入結束。</div><div class="panel_bottom" style="height: auto; margin: 0px; font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center; background: url("panel-bottom.png") 0% 0% no-repeat;"> </div><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background: url("panel-title.png") 0% 100% no-repeat transparent;">Output</div><div class="panel_content" style="height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background: url("panel-content.png") repeat-y;">每組只輸出一個整數,其單獨成行,該整數代表一篇文章裡不同單詞的總數。</div><div class="panel_bottom" style="height: auto; margin: 0px; font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center; background: url("panel-bottom.png") 0% 0% no-repeat;"> </div><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background: url("panel-title.png") 0% 100% no-repeat transparent;">Sample Input</div><div class="panel_content" style="height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background: url("panel-content.png") repeat-y;"><pre style="word-wrap: break-word; white-space: pre-wrap; margin-top: 0px; margin-bottom: 0px;"><div style="font-family: 'Courier New', Courier, monospace;">you are my friend
#</div>

Sample Output 4

這題目如果知道set和函式strtok的用法,那麼久很簡單了;

其實不用函式,自己寫也可以,不過程式碼會很長而已,還容易出錯;

程式碼:

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<set>
using namespace std;
char str[10000];
int main()
{
    set<string>S;
    string temp;
    while(cin.getline(str,10000)){
        if(str[0]=='#') break;
        S.clear();
        char *temp=strtok(str," ");
        while(temp!=NULL){
            if(S.count(temp)==0) S.insert(temp);
            temp=strtok(NULL," ");
        }
        cout<<S.size()<<endl;
    }
    return 0;
}