Andy's First Dictionary (Andy's First Dictionary, VA 10815), Andy 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 wowould pick one of his favorite story books, from which he wowould 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 shoshould give a list of different words that appears in the input text, one in a line. the words shoshould 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 DisneylandTwo blondes were going to Disneyland when they came to a fork in theroad. The sign read: "Disneyland Left."So they went home.
Sample Output
aadventuresblondescamedisneylandforkgoinghomeinleftreadroadsignsothetheytotwowentwerewhen
The general Chinese meaning is: enter a text to find all the different words (consecutive letter sequences), output by dictionary sequence from small to large. Words are case-insensitive. The input and output are the same as those above.
This question is relatively simple. I learned the code of the book "Getting started to algorithm competitions.
1. Learn to use the set container. (There is a small knowledge point. The set container and elements can only appear once, and insertion can be sorted from small to large)
2. Learn common functions in the character function library
3. Learn stringstream (refer to this blog: http://blog.csdn.net/xw20084898/article/details/21939811)
4. Remember to press ctrl + z + press enter on the empty line. (For why, refer to blog: http://blog.csdn.net/kevin_ut/article/details/8576740)
Set is a set of elements that are not repeated in the same set as those of the conto predecessors and the elements in the set are ordered (automatically ordered). TY said that its internal essence is a Balance Tree.
Set is not an array. Only the iterator (iterator) is used to pour out the elements in it. The iterator is equivalent to scanning the address. Therefore, * variation is required for output.
Header file set is required
Set <string> dict; create a set named dict. The base type is string.
Dict. begin () dict. end () returns the address of the first and last elements of the set.
This is used to write this example.
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 }
It is not a problem to understand the English comments.
Isalpha () determines whether it is a letter. If it is a letter, convert tolower () in the header to lowercase. Otherwise, convert it to a space. This is used to remove spaces when rerunning the string stream.
Special emphasis on the iterator used in this example
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
The iterator is equivalent to a type. it is a variable base type. it is the iterator, which is the first to sweep from the beginning to the end. [according to TY bacteria, like an array, the last is an array without values. it is an end sign. I don't know what the last part of the set is.] The output is because the it scans the address to output * it
This seems to have used the stl set syntax ....
Obviously, I still know little about set and need to keep learning in the future.
This evening is now. After writing a blog, I will attend classes tomorrow .....
This article partially references http://blog.csdn.net/ametake