24h購物| | PChome| 登入
2012-06-05 21:51:06| 人氣1,068| 回應0 | 上一篇 | 下一篇

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

推薦 0 收藏 0 轉貼0 訂閱站台

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

台長: Morris
人氣(1,068) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][殺人系列] 133 - The Dole Queue
此分類上一篇:[UVA][map] 10815 - Andy's First Dictionary

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文