2012-06-05 21:51:06Morris

[UVA][map] 11062 - Andy's Second Dictionary

Problem G - Andy's Second Dictionary

Time Limit: 1 second

Andy is now 9-year old and is getting ambitious. He wants to become the world largest dictionary editor. You have already helped him to solve a problem with a computer and he now turns back to you with a new challenge. He already has a program that copies all the words from a text and outputs them in alphabetical order. Unfortunately, this program does not take into account hyphenation and is not satisfying him. He wants you to write a new program that can copy words from a text, even when those words are hyphenated.

The input file is a text with no more than 500 words of arbitrary length. Input is terminated by EOF.

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. Also words may be hyphenated. When a word is hyphenated, its prefix initiates on a line, followed by hyphen character, maybe followed by a newline, followed by the rest of the word. The rest of the word may be itself hyphenated. The hyphen character is part of the word if, and only if, not followed by a newline.

Input

The input file is a text 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.

Sample Input

Adv-
ent-
ures
in
Dis-
ney-
land

Two blondes were go-
ing  to  Disney-land
when they  came to a
fork  in  the  road.
The sign read: "Dis-
neyland Left."

So they went home.

Sample Output

a
adventures
blondes
came
disney-land
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when

這題多了 - 連接符號, 因此多一步於是否下一個字元是換行的判斷

#include <stdio.h>
#include <iostream>
#include <map>
using namespace std;

int main() {
int i, idx = 0;
map<string, char> record;
char ch, str[200];
while(1) {
ch = getchar();
if(ch == EOF) break;
if(ch >= 'A' && ch <= 'Z')
ch = ch - 'A' + 'a';
if(ch >= 'a' && ch <= 'z') {
str[idx++] = ch;
} else if(ch != '-') {
str[idx++] = '\0';
if(idx > 1) {
record[str] = 1;
}
idx = 0;
} else {
ch = getchar();
if(ch != '\n') {
str[idx++] = '-';
if(ch >= 'A' && ch <= 'Z')
ch = ch - 'A' + 'a';
str[idx++] = ch;
}
}
}
for(map<string, char>::iterator i = record.begin(); i != record.end(); i++)
puts(i->first.c_str());
return 0;
}