安迪的第一個字典(Andy's First Dictionary,UVa 10815)。本站提示廣大學習愛好者:(安迪的第一個字典(Andy's First Dictionary,UVa 10815))文章只能為提供參考,不一定能成為您想要的結果。以下是安迪的第一個字典(Andy's First Dictionary,UVa 10815)正文
Description
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.
InputThe 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 when1.中文翻譯
大體的中文意思就是:輸入一個文本,找出所有不同的單詞(連續的字母序列),按字典序列從小到大輸出。單詞不區分大小寫,輸入輸出和上面一樣。
2.分析
這個題比較簡單,我學習的是《算法競賽入門經典》這本書的代碼。
1.學會運用set容器;(有個小知識點,set容器,元素只能出現一次,並且插入可以從小到大排序)
2.學習字符函數庫中常用的函數
3.學會stringstream(可參考這篇博文:http://blog.csdn.net/xw20084898/article/details/21939811)
4.最後運行記得是 在空行 ctrl+z +回車。(至於為什麼,參考博文:http://blog.csdn.net/kevin_ut/article/details/8576740)
set是一個集合 和康托前輩的一樣 集合中的元素不重復 且集合中的元素是有序的(自動有序化) TY菌介紹說其內部實質是一個平衡樹
set不是數組 只能通過迭代器(iterator)把裡面的元素倒出來 迭代器相當於是指針 掃描的是地址 因此輸出的時候需要用*variation
4.聲明
需要用到頭文件set
set<string> dict; 建立一個集合 其名為dict 基類型是string
5.常用函數
dict.begin() dict.end()返回集合最初和最後的元素的地址
這是寫這個例題用到的
6.代碼
1 #include<iostream> 2 #include<string> 3 #include<set> 4 #include<sstream> 5 using namespace std; 6 7 set<string> dict;//set up a set called dict-short for dictionary,and it's based on string type; 8 9 int main(){ 10 string s,buf; 11 while (cin>>s){ 12 for (int i=0;i<s.length();i++) 13 if (isalpha(s[i])) s[i]=tolower(s[i]);//if it's an letter,turn it lowercase.Others turn space and use stringstream to leave it out=ignore it 14 else s[i]=' '; 15 stringstream ss(s); 16 while (ss>>buf) dict.insert(buf);//insert it into the set which is already in order,TYkon said it's a balanced tree inside 17 } 18 for (set<string>::iterator it=dict.begin();it!=dict.end();++it)//iterator just like a point,scan it from beginning to end and output 19 cout<<*it<<endl;//NOTICE output by point 20 return 0; 21 }
還是英文注釋不過看懂應該問題不大
isalpha()判斷是否是字母 如果是就用頭tolower()轉換成小寫 否則就轉為空格 這樣是為了字符串流重讀入的時候能夠把空格去掉
特別強調一下本例中出現的迭代器
1 for (set<string>::iterator it=dict.begin();it!=dict.end();++it)//iterator just like a point,scan it from beginning to end and output 2 cout<<*it<<endl;//NOTICE output by point
迭代器iterator相當於是個類型 it是一個變量 基類型是iterator 它從開始掃到結尾的前一個【據TY菌解釋 像數組一樣 最後一位是沒有值的 數組是一個結束標志 不知集合最後是什麼】輸出的時候 由於it掃描的是地址 要輸出*it
這個好像用到了stl set的語法....
顯然對於set我還知之甚少 還需要在今後的學習中不斷掌握
今天晚上此時已是0:15,寫完博客,明天上課.....
本文部分引用http://blog.csdn.net/ametake