UVA 10815 Andy's First Dictionary【set】

题目链接:https://vjudge.net/contest/211547#problem/C

 

题目大意:

输入一个文本,找出所有不同的单词(连续的字母序列),按字典序从小到大输出,单词不区分大小写。

 

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

……

 

 

#include<iostream>
#include<string>
#include<set>
#include<sstream>
using namespace std;

set<string> dict;
string s, buf;

int main() {
  while(cin >> s) {
    for(int i = 0; i < s.length(); i++)
      if(isalpha(s[i])) s[i] = tolower(s[i]); else s[i] = ' ';          //一个一连串的字符串,如果带有其它符号,就会被分割成几个字符串
    stringstream ss(s);             //形成可能带有空格的字符串流
    while(ss >> buf) dict.insert(buf);         //将字符串流中的单词一个一个的存储在集合内
  }
  for(set<string>::iterator it = dict.begin(); it != dict.end(); ++it)                //set具有给集合内元素去重和自动排序的功能
    cout << *it << "\n";
  return 0;
}

 

2018-04-05

posted @ 2018-04-05 12:46  悠悠呦~  阅读(174)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end