10815-andy ' s Dictionary
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=96&page=show_problem &problem=1756
Andy, 8, has a dream-he wants to produce his very own. This isn't a easy task for him, as the number of words which is, a, not quite enough. Instead of thinking up all the words himself, he has a briliant idea. From him bookshelf he would pick one of his favourite story books, from which to he would copy of the distinct. By arranging the "words in alphabetical", he is done! Of course, it is a really time-consuming job, and that is where a computer the program is helpful.
You are are asked to write a program which lists all the different words in the input text. In this problem, a word was defined as a consecutive sequence of alphabets, in upper and/or. Words with only one letter are also to be considered. Furthermore, your program must is case insensitive. For example, words like "apple", "apple" or "apple" must is considered the same.
Input
The input file is a-text with no more than 5000 lines. An input line has at most characters. The Input is terminated by EOF.
Output
Your output should give a list of different words that appears into the input text, one in a line. The words should all is in lower case and sorted in alphabetical order. You can is sure that him number of distinct words in the text does not exceed 5000.
Sample Input
Adventures in Disneyland
Two blondes were going to Disneyland while 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
STL implementation: (Set Implementation)
/*0.029s*/
#include <cstdio>
#include <string>
#include <set>
using namespace std ;
set<string>s;
Set<string>::iterator it;
String str;
int main (void)
{
char ch;
while (~ (ch = getchar ()))
{
if (!isalpha (CH))
{
if (str!= "")
S.insert (str);
str = "";
}
else
str = tolower (ch);
}
for (it = S.begin (); it!= s.end (); ++it)
puts ((*it). C_STR ());
return 0;
}
Non-STL implementation: (but will re, do not know why)
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
using namespace std;
Char word[5010][210], *p[5010], ch;
inline bool Getword (char *s)
{while
!isalpha (ch = getchar ())
if (ch = = 1) return false;
int i = 0;
s[i++] = tolower (CH);
while (isalpha (ch = getchar ()))
s[i++] = tolower (CH);
S[i] = ' the ';
return true;
}
BOOL CMP (char *a, char *b)
{return
strcmp (A, B) < 0;
}
int main (void)
{
int n;
for (n = 0; Getword (Word[n]); ++n)
p[n] = word[n];
Sort (p, p + N, CMP);
if (n) puts (p[0]);
for (int i = 1; i < n; ++i)
if (strcmp (p[i), p[i-1))
puts (p[i));
return 0;
}