Problem B: Andy's First Dictionary |
Time limit: 3 seconds |
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
Joke and image taken from the Web
It has been wa for a long time. The original idea is to read one row at a time and then read words one by one in one row. Words are stored in a single-link table. But it never passes. After reading other people's code, I changed to receive characters one by one and finally AC = very hard. The Code is as follows:
# Include <stdio. h> # include <string. h> char word [5000] [200]; int main (void) {char ch, buf [200]; int pos = 0, flag = 0; int n_word = 0; while (ch = getchar ())! = EOF) {if (isalpha (ch) {/* is the letter */flag = 1; buf [pos ++] = tolower (ch );} else {if (flag) {/* end of a word */buf [pos] = '\ 0'; pos = 0; flag = 0; int I = 0, j, stat; while (I <n_word & (stat = strcmp (buf, word [I])> 0) ++ I; /* locate the position to be inserted */if (I = n_word) {/* Insert at the last position */strcpy (word [I], buf); + + n_word ;} else if (! Stat)/* equal to a string */continue; else {for (j = n_word; j> I; -- j) strcpy (word [j], word [J-1]); strcpy (word [j], buf); ++ n_word ;}}} int I; for (I = 0; I <n_word; ++ I) printf ("% s \ n", word [I]); return 0 ;}