此题为2011年9月28日华为长沙站上机试题,难度不大,但要在短时间内调试正确符合题意还是需要一定的基础。本文给出C和C++两种实现,代码没有进行优化,不过基本功能已经实现,欢迎大家一起探讨。
题目:删除一个字符串中出现次数最少的字符,函数原型为:
char * delChar(char *s,int iLen) 其中 s为输入字符串,iLen为输入字符串长度。如输入字符串为“abcdd”,输出为"dd"。字符串中只有小写字母,不含空格且字符串最大长度不超过20。
C语言实现:使用数组和链表实现
// a1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct szChar
{
char data;
struct szChar *next;
};
char* delMinChar(char *s,int iLen)
{
char *temp = s;
int i=0;
int j=0;
int count[26]={0};
int minCount=21;
char filter[21]={'\0'};
int filterLen=0;
while(temp[i]!='\0')
{
count[temp[i]-'a']++;
i++;
}
i=0;
while(i<26)
{
if(count[i]<minCount&&count[i]>0)
{
minCount = count[i];
}
i++;
}
i=0;
while(i<26)
{
if(count[i]==minCount)
filter[j++]=i+'a';
i++;
}
filterLen = j;
char *result=(char*)malloc(sizeof(char)*iLen);
struct szChar *head = (szChar*)malloc(sizeof(struct szChar));
head->data=temp[0];
head->next=NULL;
struct szChar *p = head;
struct szChar *prev=NULL;
struct szChar *del =NULL;
i=1;
while(temp[i]!='\0')
{
struct szChar *cur = (struct szChar*)malloc(sizeof(struct szChar));
cur->data=temp[i];
cur->next = NULL;
p->next = cur;
p = cur;
i++;
}
for(j=0;j<filterLen;j++)
{
prev=head;
p=prev->next;
if(head->data==filter[j])
{
free(head);
head = p;
continue;
}
while(p)
{
if(p->data==filter[j])
{
prev->next = p->next;
del=p;
p=p->next;
free(del);
}
}
}
p=head;
i=0;
while(p)
{
result[i++]=p->data;
p=p->next;
}
result[i++]='\0';
printf("after delete the string is : %s\n",result);
return result;
}
int main(int argc, char* argv[])
{
char *originString ="abcdd";
printf("origial string is : %s\n",originString);
char *output = delMinChar(originString,5);
printf("after delete the string is : %s\n",output);
return 0;
}
C++实现:使用顺序容器和关联容器
// a1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <utility>
#include <map>
#include <string>
#include <vector>
using namespace std;
char* delMinChar(char *s,int iLen)
{
map<char,int>smap;
vector<char>svec;
vector<char>originString;
int i=0;
int minCount=21;
while(s[i])
{
originString.push_back(s[i]);
++smap[s[i]];
++i;
}
for(map<char,int>::iterator it=smap.begin();it!=smap.end();++it)
{
if(it->second<minCount)
minCount = it->second;
cout<<it->first<<" :count "<<it->second<<" times"<<endl;
}
cout<<"Min Count is "<<minCount<<endl;
cout<<"Min Count char is :"<<endl;
for(it=smap.begin();it!=smap.end();++it)
{
if(it->second==minCount)
{
cout<<it->first<<" ";
svec.push_back(it->first);
}
}
cout<<endl;
for(vector<char>::iterator it2=svec.begin();it2!=svec.end();it2++)
for(vector<char>::iterator it3=originString.begin();it3!=originString.end();it3++)
{
if(*it3==*it2)
{
originString.erase(it3);
}
}
char *result = (char*)malloc(sizeof(char)*21);
int index=0;
for(vector<char>::iterator it3=originString.begin();it3!=originString.end();it3++)
{
result[index++]=*it3;
}
result[index++]='\0';
return result;
}
int main(int argc, char* argv[])
{
char *originString ="abcdd";
printf("origial string is : %s\n",originString);
char *output = delMinChar(originString,5);
printf("after delete the string is : %s\n",output);
return 0;
}