您的位置:首页 > 其它

算法提高 11-2删除重复元素

2017-03-27 19:16 375 查看
  算法提高 11-2删除重复元素  

时间限制:10.0s   内存限制:256.0MB
    

问题描述

  为库设计新函数DelPack,删除输入字符串中所有的重复元素。不连续的重复元素也要删除。

  要求写成函数,函数内部使用指针操作。

样例输入

1223445667889

样例输出

13579

样例输入

else

样例输出

ls

数据规模和约定

  字符串数组最大长度为100。

想到的第一个是用map做,能够去掉重复元素,但是我忘记了map有自动排序的功能,好吧,之前是觉得只有set有呢,后来只能把它存到一个vector数组里,如果发现有重复的元素,就用vector的函数erase去掉,用#include<algorithm>中的find函数找到这个s[i],去掉就可以了,这题真是充分复习了stl库,汗颜。

用map做的自动排序:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
string s;
cin>>s;
map<char,int> m;
for(int i=0;i<s.length();i++)
{
m[s[i]]++;
}
map<char,int>::iterator it;
for(it=m.begin();it!=m.end();it++)
{
if(it->second==1)
{
printf("%c",it->first);
}
}
return 0;
}
ac代码如下
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
string s;
cin>>s;
map<char,int> m;
vector<char>b;
for(int i=0;i<s.length();i++)
{
if(m[s[i]]==0)
{
b.push_back(s[i]);
m[s[i]]=1;
}
else
{
b.erase(find(b.begin(),b.end(),s[i]));
}
}
for(int i=0;i<b.size();i++)
{
printf("%c",b[i]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: