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


题目大意:输入一段文本(不超过5000行),按字典序把每个单词排序(需要去重)。

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 
 5 char s[50005][205];                   //数组要开大,否则就runtime error了
 6 
 7 int isLetter(char &ch)
 8 {
 9     if (ch >= 'a' && ch <= 'z')
10         return 1;
11     if (ch >= 'A' && ch <= 'Z')
12     {
13         ch = ch + 'a' - 'A';             //变小写
14         return 1;
15     }
16     return 0;                                      //根据返回的是0还是1判断是不是字母
17 }
18 
19 int cmp(const void * a, const void * b)
20 {
21     return strcmp((char *) a, (char *) b);       //要熟练运用啊(字符串按字典序排序)
22 }
23 
24 int main()
25 {
26     char c;
27     int flag = 0, i = 0, j = 0;
28     while (scanf("%c", &c) != EOF)   //一个字符一个字符的输入,这是非常简单、有条理、不容易乱的处理方法。之前没用过,要学会使用。
29     {
30         if (isLetter(c) && !flag)     //如果该字符是字母且是第一个字母的话
31         {
32             j = 0;                    //当然从j = 0开始记录单词的第一个字母了
33             s[i][j++] = c;
34             flag = 1;
35         }
36         else if (isLetter(c) && flag)
37         {
38             s[i][j++] = c;
39         }
40         else if (!isLetter(c) && flag)  //如果该字符不是字母的话
41         {
42             s[i][j] = '';            //记录该单词(该单词结束),一定不要忘了加''
43             i++;                       //开始准备记录下一个单词
44             flag = 0;
45         }
46     } 
47     qsort(s,i,sizeof(s[0]),cmp);       //按字典序排序,学会使用qsort,便捷
48     for (j = 0; j < i - 1; j++)
49     {
50         if (strcmp(s[j], s[j + 1]) == 0)    //去重操作(因为都排好序了),相同的挨在一起
51             continue;
52         printf("%s
", s[j]);
53     }
54     printf("%s
", s[j]);                 //最后一个单独输出
55     return 0;
56 }




原文地址:https://www.cnblogs.com/youdiankun/p/3302145.html