UVa 10815 Andy's First Dictionary

感觉这道题要比之前几个字符串处理的题目难度要大了一些。

 

题目大意:给若干行字符串,提取出所有单词并去掉重复的,最后按字典顺序输出。

 

对于输入大致有两种思路,一种是逐个读入字符,遇到字母的话就放到word里面,直到遇到非字母字符,在word最后放'\0'。

我采用第二种思路,就是用gets()函数逐行读到str字符数组里面,然后逐个把单词提取出来。

Count存放的是Dictionary里单词的个数,每提取一个单词,看看Dictionary里有没有该单词,没有的话放到字典里面,并且计数器加1。

由于功力不够深厚,最后对Dictionary排序的时候不会写cmp函数,因此用不了qsort(),所以又笨笨的写了个冒泡排序,居然没有超时。

 

 

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 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

 

国际惯例,AC代码如下:

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 char Dictionary[5005][40];//用来存放单词
 9 int Count = 0;                  //不重复的单词的个数
10 char str[215], word[40];
11 
12 int cmp(const void *a, const void *b);
13 
14 int main(void)
15 {
16     #ifdef LOCAL
17         freopen("10815in.txt", "r", stdin);
18     #endif
19 
20     int i, j;
21     while(gets(str))
22     {
23         int l = strlen(str);
24         if(l == 0)
25             continue;
26         for(i = 0; i < l; ++i)
27             str[i] = tolower(str[i]);
28         i = 0;
29 
30         while(i < l)
31         {
32             if((i == 0 || str[i - 1] < 'a' || str[i - 1] > 'z')//判断一个单词的开始
33                 && str[i] >= 'a' && str[i] <= 'z')
34                 word[0] = str[i];
35             else
36             {
37                 ++i;
38                 continue;
39             }
40 
41             ++i;
42             int j = 1;
43             while(str[i] >= 'a' && str[i] <= 'z' && i < l)
44             {
45                 word[j++] = str[i++];
46             }
47             word[j] = '\0';
48 
49             for(j = 0; j < Count; ++j)
50             {
51                 if(!strcmp(Dictionary[j], word))
52                     break;
53             }
54             if(j == Count)
55                 strcpy(Dictionary[Count++], word);
56             }
57     }
58     
59     for(i = Count - 1; i > 1; --i)//不会用qsort,只能恶心地写个冒泡了
60     {
61         for(j = 0; j < i; ++ j)
62         {
63             if(strcmp(Dictionary[j], Dictionary[j + 1]) > 0)
64             {
65                 char t[30];
66                 strcpy(t, Dictionary[j]);
67                 strcpy(Dictionary[j], Dictionary[j + 1]);
68                 strcpy(Dictionary[j + 1], t); 
69             }
70         }
71     }
72     for(i = 0; i < Count; ++i)
73         cout << Dictionary[i] << endl;
74     return 0;
75 }
代码君

最后去网上查了一下别人写的cmp函数,试了一下同样AC!

1 int cmp(const void *a, const void *b)
2 {
3     return strcmp((char *)a, (char *)b);
4 }
5 qsort(Dictionary, Count, sizeof(Dictionary[0]), cmp);

 


以上是我几个月前写的代码,现在看起来真挫啊!学习一下紫书STL的用法。最大的特点就是用起来方便而且代码简短不容易出错,而且速度也比我的代码快多了,以上我手写的冒泡排序运行时间为0.256s,加上快排的话大概能缩短到0.1s左右,但这份STL代码仅仅0.062s

set就是数学上的集合,每个元素都不重复。同sort一样,自定义类型也可以构造set,但同样必须定义“小于”运算符。

 

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <set>
 4 #include <string>
 5 #include <sstream>
 6 using namespace std;
 7 
 8 set<string> dict;
 9 
10 int main(void)
11 {
12     #ifdef LOCAL
13         freopen("10815in.txt", "r", stdin);
14     #endif
15 
16     string s, buf;
17     while(cin >> s)
18     {
19         for(int i = 0; i < s.length(); ++i)
20             if(isalpha(s[i])) s[i] = tolower(s[i]);    else s[i] = ' ';
21         stringstream ss(s);
22         while(ss >> buf)    dict.insert(buf);
23     }
24     for(set<string>::iterator it = dict.begin(); it != dict.end(); ++it)
25         cout << *it << "\n";
26 
27     return 0;
28 }
飞快的代码君

 

 

 

 

 

posted @ 2014-07-01 08:59  AOQNRMGYXLMV  阅读(215)  评论(0编辑  收藏  举报