UVA10815 安迪的第一个字典 Andy‘s First Dictionary

简介: UVA10815 安迪的第一个字典 Andy‘s First Dictionary

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

输入样例

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.

输出样例

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

代码

#include<iostream>
#include<algorithm>
#include<sstream>
#include<string>
#include<set>//因为可能有重复的单词,而set可以去重,使用起来更好。
using namespace std;
string str,str2;
stringstream ss;
set<string> jh;
int main() {
  while (cin >> str ) {
    for (int i = 0; i < str.length(); i++) {//去除非单词字符
      if (isalpha(str[i])) {
        str[i] = tolower(str[i]);
      }
      else {
        str[i] = ' ';
      }
    }
    ss << str; //每次得清空ss
    //stringstream ss(str);  如果这样写就不用每次清空ss了.
    while (ss >> str) 
      jh.insert(str);
    ss.clear();//这里必须清空...具体原因为止.
  }
  for (set<string>::iterator it = jh.begin(); it != jh.end(); it++) {
    cout << *it << endl;
  }
  return 0;
}
相关文章
|
1天前
|
存储 Swift
在Swift编程语言中,字典(Dictionary)
在Swift编程语言中,字典(Dictionary)
40 3
|
7月前
|
存储 Java Python
多重字典(Multi-Level Dictionary)
多重字典(Multi-Level Dictionary)是一种将多个字典组合在一起的数据结构,用于解决需要在多个维度上查找数据的问题。多重字典可以看作是一个嵌套的字典,每个字典都可以作为其他字典的键。 使用多重字典的场景:
55 3
|
1天前
|
开发者 Python
【Python 基础】递推式构造字典(dictionary comprehension)
【5月更文挑战第8天】【Python 基础】递推式构造字典(dictionary comprehension)
|
1天前
|
存储 算法 数据库
Python字典(Dictionary)
Python字典(Dictionary)
12 0
|
1天前
|
存储 缓存 数据库
python中的字典(Dictionary)
python中的字典(Dictionary)
20 0
|
1天前
|
存储 Swift
在Swift编程语言中,字典(Dictionary)
在Swift编程语言中,字典(Dictionary)
381 3
|
1天前
|
Python
在Python中,字典(dictionary)的键(key)具有唯一标识性
在Python中,字典(dictionary)的键(key)具有唯一标识性
55 1
|
1天前
|
存储 Python
在Python中,字典(Dictionary)
在Python中,字典(Dictionary)
31 1
|
5月前
|
存储 JSON Shell
Python(十八)python字典dictionary
Python中的字典和json对象类似,都是键值对存储数据。 但是,其二者是有区别的。只是类似,并不一样。 字典和json的区别,后边会单独提到。 Python字典: 1. 字典是列表之外另一种可变容器模型,且可存储任意类型对象。 2. 字典以键值对{key:value}形式存储。 3. 键必须是唯一的,不允许同一个键出现两次。但值则不必。 **4. ** 值可以取任何数据类型,但键必须是不可变的,如字符串,数字。 **5. ** 定义字典使用一对大括号 {} 来定义。 **6. ** 字典是一个无序的数据集合,我们更关心key对应的值,而不是关心其存储的顺
55 0
|
8月前
|
C#
C#中字典Dictionary的用法详解
C#中字典Dictionary的用法详解