UVaOJ 10815 - Andy's First Dictionary

AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 1. Elementary Problem Solving :: String


Description

8岁的小 Andy 想要自己写一本字典,但是他认识的字不太多。

于是他就将他最喜欢的一个故事里面的单词,整理成一本字典。


单个或多个连续的字母算作一个单词,

所有单词在字典中都小写。

且字典按字典序排序(废话)。


Type

String


Analysis

相当于读一篇文章,找出其中的所有单词并且按字典序排序输出。

可以利用 STL 中的 set。

找到单词即插入 set 中,则 set 自动排序这些单词。

最后通过迭代器按顺序输出单词即可。


Solution

// UVaOJ 10815
// Andy's First Dictionary
// by A Code Rabbit

#include <cctype>
#include <cstdio>
#include <string>
#include <set>
using namespace std;

char ch;

int main() {
    set<string> dictionary;
    string word;
    while (scanf("%c", &ch) != EOF) {
        if (!isalpha(ch)) {
            if (word != "") dictionary.insert(word);
            word = "";
        } else {
            word += tolower(ch);
        }
    }
    for (set<string>::iterator iter = dictionary.begin();
        iter != dictionary.end();
        ++iter)
    {
        puts(iter->c_str());
    }

    return 0;
}
 

你可能感兴趣的:(UVaOJ 10815 - Andy's First Dictionary)