字符集合

输入一个字符串,求出该字符串包含的字符集合
链接:链接
来源:牛客网

常规

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str;
    while(cin>>str)
    {
        int a[124] = {0};
        char b[54] = {0};
        int j = 0;
        for(int i=0; i<str.size(); i++)
        {
            if(a[str[i]] == 0)
            {
                a[str[i]] = 1;
                b[j] = str[i];
                j++;
            }
        }
        cout<<b<<endl;
    }
    return 0;
}

哈希表

思路和上面的一样

#include<iostream>
#include<string>
#include<map>
using namespace std;
int main(){
    string s;
    while(cin>>s){
        string res;
        map<char,int> cnt;
        for(int i=0;i<s.size();i++){
            if(cnt[s[i]]==0){
                cnt[s[i]]++;
                res+=s[i];
            }
        }
        cout<<res<<endl;
    }
}

posted @ 2019-09-01 20:53  煊奕  阅读(238)  评论(0编辑  收藏  举报