题目描述
输入一行单词序列,相邻单词之间由 1 个或多个空格间隔,
请按照字典序输出这些单词,要求重复的单词只输出一次,区分大小写;
输入格式
一行单词序列,最少 1 个单词,最多 100 个单词,每个单词长度不超过 50,单词之间用至少 1 个空格间隔。
数据不含除字母、空格外的其他字符。
输出格式
按字典序输出这些单词,重复的单词只输出一次。
输入样例
She wants to go to Peking University to study Chinese
输出样例
Chinese
Peking
She
University
go
study
to
wants
题解
排序 && 哈希表:
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
const int N = 110;
int k;
string s, str[N];
map<string, bool> Hash;
int main()
{
while(cin >> s)
{
if(Hash.count(s)) continue;
Hash[s] = true;
str[k ++] = s;
}
sort(str, str + k);
for (int i = 0; i < k; i ++) cout << str[i] << endl;
return 0;
}
ps:这种输入格式在本地运行的时候,要以 Ctrl + Z
告诉电脑输入完毕,否则是没有输出的;