uvaoj-10815:安迪的第一個字典

Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all the words himself, he has a briliant idea. From his bookshelf he would pick one of his favourite story books, from which he would copy out all the distinct words. By arranging the words in alphabetical order, he is done! Of course, it is a really time-consuming job, and this is where a computer program is helpful.

You are asked to write a program that lists all the different words in the input text. In this problem, a word is defined as a consecutive sequence of alphabets, in upper and/or lower case. Words with only one letter are also to be considered. Furthermore, your program must be CaSe InSeNsItIvE. For example, words like "Apple", "apple" or "APPLE" must be considered the same.

 

Input

The input file is a text with no more than 5000 lines. An input line has at most 200 characters. Input is terminated by EOF.

 

Output

Your output should give a list of different words that appears in the input text, one in a line. The words should all be in lower case, sorted in alphabetical order. You can be sure that he number of distinct words in the text does not exceed 5000.

 

Sample Input

Adventures in Disneyland

Two blondes were going to Disneyland when they came to a fork in the
road. The sign read: "Disneyland Left."

So they went home.

 

Sample Output

a
adventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when

 

題解:來自劉汝佳112頁set的例題。做這個題的方法也是劉汝佳的方法,這次先做筆記,後貼代碼,因爲和ac比起來,得到的教訓纔是更重要的吧;

筆記:迭代器在c++中相當於指針;set集合中的元素是自動有序排列的,用時也是唯一的;迭代器中大部分都沒有“<”或“>”運算符;string,vector,set,map的確比傳統c更加方便,但也更難掌握,之後的聯繫中加強這方面的練習;
//sstream頭文件帶來的stringstream 的流輸入在分割單詞的時候真心好用的不得了;
//汝佳大法好,set不得了;Orz。。


code:
#include <iostream>
#include <set>
#include <string>
#include <sstream>
#include <cstdio>
using namespace std;

set<string> dic;

int main()
{
    string str,ans;
    while(cin>>str)
    {
        int len=str.size();
        //cout<<len;
        for(int i=0; i<len; i++)
        {
            if(isalpha(str[i]))
            {
                str[i]=tolower(str[i]);//必須用等式來重新賦值;
            }
            else
            {
                str[i]=' ';//將標點符號轉變爲可以被cin識別的空格;
            }
        }
        stringstream ss(str);//創建流;
        while(ss >> ans)
        {
            dic.insert(ans);
        }
    }
    set<string>::iterator it;//定義迭代器;
    for(set<it=dic.begin(); it!=dic.end(); ++it)//循環中這種寫法最好
    {
        cout<<*it<<endl;//迭代器相當於指針,意思是用法和指針類似;
    }
    return 0;
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章