您的位置:首页 > 编程语言 > C语言/C++

华为:输入一个字符串,删除重复前面的字符,其余字符按原样顺序输出

2013-04-16 20:37 465 查看
/*
* 输入一个字符串,删除重复前面的字符,其余字符按原样顺序输出.
* 例如:
* 输入: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
={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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  CC++ 笔试面试
相关文章推荐