且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

[华为机试真题][2014]62.去除重复字符并排序

更新时间:2022-08-12 18:45:00

题目

描述:

去除重复字符并排序

运行时间限制:

无限制

内容限制:

无限制

输入:

字符串

输出:

去除重复字符并排序的字符串

样例输入:

aabcdefff

样例输出:

abcdef

代码

/*---------------------------------------
*   日期:2015-07-05
*   作者:SJF0115
*   题目:去除重复字符并排序
*   来源:华为机试真题
-----------------------------------------*/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

string RemoveDuplicateAndSort(string str){
    // 排序
    sort(str.begin(),str.end());
    string::iterator ite = str.begin();
    string::iterator next;
    // 删除重复
    while(ite != str.end()){
        next = ite+1;
        if(*next == *ite){
            str.erase(next);
        }//if
        else{
            ++ite;
        }//else
    }//while
    return str;
}

int main(){
    string str;
    //freopen("C:\\Users\\Administrator\\Desktop\\acm.in","r",stdin);
    while(getline(cin,str)){
        cout<<RemoveDuplicateAndSort(str)<<endl;
    }//while
    return 0;
}