【UVA

题目:

Input

测试数据将输入一篇文章。不超过5000行,每一行最多200个字符,并以EOF结束。

Output

按照字典序输出他学到的单词,每行输出一个单词,输出单词时所有的字母全部小写。 
数据保证最多有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

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cctype>
#include <set>
#include <sstream>
using namespace std;
string b;
set<string> dict;
//直接用set避免重复,大小写是同一个单词,那么就预处理,都变成小写
//有可能单词不以空格分隔,所以非字母都是分隔符;
//有两种做法,一种把特殊字符变成空格,然后重新构造输入流
//第二种是手动分隔

//1.手动
int main(){
    int c=0,flg=0,start=0,end=0,i;
    // while (cin>>b)
    // {
    //     b=b+"*";//预处理
    //     //下一个字符的开始添加上一个单词
    //     for(i=0;i<b.length();i++)
    //         if(isalpha(b[i])) break;
    //     start=i-1;
    //     for(;i<b.length();i++){
    //         if(isalpha(b[i])){
    //             end=i;
    //             b[i]=tolower(b[i]);
    //             flg=0;
    //         }
    //         else{
    //             if(!flg){
    //                 flg=1;
    //                 dict.insert(b.substr(start+1,end-start));//注意substr 是 offset,长度
    //             }
    //             start=i;
    //         }
    //     }
    // }

    //2.使用字符串流
    while (cin>>b)
    {
        for(int i=0;i<b.length();i++)
            if(isalpha(b[i]))
                b[i]=tolower(b[i]);
            else b[i]=' ';
        stringstream stream(b);//构造字符串流来输入,以空格为分割
        string buf;
        while (stream>>buf)
            dict.insert(buf);
    }
    for(set<string>::iterator i=dict.begin();i!=dict.end();i++)
        cout<<*i<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/MorrowWind/p/UVA-10815-Andys-First-Dictionary.html