/*
* 输入一个字符串,删除重复前面的字符,其余字符按原样顺序输出.
* 例如:
* 输入:input: "acbdfdffZZet";
* 输出:output:"acbdfZet";
* 实现函数:String(String input)
*/
//你理解错题意了,你的做法是只保留出现一次的字符
//我想说的是,这道题目有歧义,删除重复前边 的字符,前边只的是什么恶?
#include<iostream>
#include<cstdio>
#include<assert.h>
#include<cstring>
using namespace std;
const int N=256;
char hash[N]={0};
void stringFilter(char *pInputStr)
{
assert(pInputStr!=NULL);
const char *p=pInputStr;
int n=strlen(pInputStr);
char *pOutputStr=new char[n+1];
if(pOutputStr==NULL)
{
cout<<"error!"<<endl;
return ;
}
while(*p!='\0')
{
hash[*p]++;
p++;
}
p=pInputStr;
char *pOut=pOutputStr;
while(*p!='\0')//重大错误,因为上一个循环已经把*pInputSt遍历到'\0'
{
if(hash[*p]>=1)
{
hash[*p]=0;
*pOut++=*p++;
}
else
{
p++;
}
}
*pOut='\0';
strcpy(pInputStr,pOutputStr);
delete [] pOutputStr;
}
void delString(char *input)
{
assert(NULL!=input);
const int max=256;
int i=0,j=0;
char hash[max]={0};
char *p=input;
while(*p)
{
hash[*p]++;
p++;
}
while(*input)
{
if(hash[*input]>=1)
{
input[j++]=input[i++];//快慢指针
hash[*input]=0;//这相还是相当于字符串去重,出现过一次以后就不在处理本字符
}
else
{
i++;
}
input++;
}
*input='\0';
}
int main()
{
char a[]="acbdfdffZZet";
cout<<"a= "<<a<<endl;
stringFilter(a);
cout<<"after stringFilter(a) a= "<<a<<endl;
return 0;
}
华为:输入一个字符串,删除重复前面的字符,其余字符按原样顺序输出
最新推荐文章于 2025-05-12 21:00:00 发布