UVA 10815 set

UVA10815 Andy's First Dictionary


題目:



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

#include <stdio.h> 
#include <string.h>
#include <string>
#include "iostream"
#include "set"
#include "sstream" 
#include "algorithm"
using namespace std;

set<string> a; 
int main() 
{
	string s;
	string word;
	while (cin>>s)
	{
		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>>word) //一行中讀入以空格結束的字符串
		{
			a.insert(word);
		}
	
			
	} 
		set<string> ::iterator it;
		for(it=a.begin();it!=a.end();it++ )
		cout <<*it<<endl;
}




set 用法 :

1.元素插入:insert()
2.中序遍歷:類似vector遍歷(用迭代器)
3.反向遍歷:利用反向迭代器reverse_iterator。
    例:
    set<int> s;
    ......
    set<int>::reverse_iterator rit;
    for(rit=s.rbegin();rit!=s.rend();rit++)
4.元素刪除:與插入一樣,可以高效的刪除,並自動調整使紅黑樹平衡。
            set<int> s;
            s.erase(2);        //刪除鍵值爲2的元素
            s.clear();
5.元素檢索:find(),若找到,返回該鍵值迭代器的位置,否則,返回最後一個元素後面一個位置。
            set<int> s;
            set<int>::iterator it;
            it=s.find(5);    //查找鍵值爲5的元素
            if(it!=s.end())    //找到
                cout<<*it<<endl;
            else            //未找到
                cout<<"未找到";
6.自定義比較函數
    (1)元素不是結構體:
        例:
        //自定義比較函數myComp,重載“()”操作符
        struct myComp
        {
            bool operator()(const your_type &a,const your_type &b)
            {
                return a.data-b.data>0; //從大到小
            }
        }
        set<int,myComp>s;
        ......
        set<int,myComp>::iterator it;
    (2)如果元素是結構體,可以直接將比較函數寫在結構體內。
        例:
        struct Info
        {
            string name;
            float score;
            //重載“<”操作符,自定義排序規則
            bool operator < (const Info &a) const
            {
                //按score從大到小排列
                return a.score<score;
            }
        }
        set<Info> s;
        ......
        set<Info>::iterator it;




發佈了22 篇原創文章 · 獲贊 1 · 訪問量 7386
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章