去除重复字符并排序
运行时间限制:无限制
内容限制: 无限制
输入: 字符串
输出: 去除重复字符并排序的字符串
样例输入: aabcdefff
样例输出: abcdef
#include<stdio.h>
#include<iostream>
#include <iterator>
#include<map>
using namespace std;
void stringFilter(const char *pInputStr, char *pOutputStr)
{
int length=strlen(pInputStr);
int j(0);
map<char,int> m;
for(int i=0; i<length; i++)
m[pInputStr[i]]++;
for( map<char,int>::iterator iter= m.begin();iter!=m.end();iter++)
pOutputStr[j++] = (*iter).first;
pOutputStr[j]='\0';
}
void stringFilter1(const char *pInputStr, char *pOutputStr)
{
if (pInputStr == NULL || pOutputStr == NULL ){
return;
}
int length = strlen(pInputStr);
if (length < 255){
int a[256]={0};//必须初始化不然内存存的是各种各样的数值
const char* pstr = pInputStr;
char* pResult = pOutputStr;
while ( *pstr != '\0'){
++a[*pstr - 'a'];
if (a[*pstr - 'a'] > 1){
pstr++;
}
else {
*pResult++ = *pstr++;
}
}
*pResult = '\0';
}
else{
cout << "Please input the 1-255 zifuchaun " << endl;
return;
}
}
int main()
{
char a[256];
strcpy(a,"aabcdefff");//bbabacacde
char b[256]="";
//stringFilter(a,b);//将字符排序后输出,不符合有些情况
stringFilter1(a,b);
cout << a << endl;
cout << "字符过滤后:\n";
cout << b << endl;
return 0;
}
测试结果:
}