2011年9月28日华为上机试题解析

本文提供了C和C++两种语言实现删除字符串中出现次数最少的字符的方法,包括使用数组和链表及顺序容器和关联容器的实现方式。详细介绍了算法流程并展示了代码实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       此题为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;
}




       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值