10:单词排序

来源:互联网 发布:dijkstra c语言 编辑:程序博客网 时间:2024/05/21 06:38

原题链接

总时间限制: 
1000ms 
内存限制: 
65536kB
描述

输入一行单词序列,相邻单词之间由1个或多个空格间隔,请按照字典序输出这些单词,要求重复的单词只输出一次。(区分大小写)

输入
一行单词序列,最少1个单词,最多100个单词,每个单词长度不超过50,单词之间用至少1个空格间隔。数据不含除字母、空格外的其他字符。
输出
按字典序输出这些单词,重复的单词只输出一次。
样例输入
She  wants  to go to Peking University to study  Chinese
样例输出
ChinesePekingSheUniversitygostudytowants

源码

#include <iostream>#include <iomanip>#include <string>#include <vector>#include <algorithm>#include <cmath>using namespace std;int main(){    string words[100];    int num = 0;    while (cin >> words[num]){        bool has = false;        for (int i=0; i<num; i++){            if (words[i].compare(words[num]) == 0){                has = true;                break;            }        }        if (!has) num++;    }    sort(words, words+num);    for (int i=0; i<num; i++){        cout << words[i] << endl;    }    return 0;}