华为笔试题13

  • 题目描述:

请设计一个自动拼写检查函数,对输入单词的错误依据字典进行修正。

 

1. 输入为一个单词和一组字典单词,每个单词长度不超过9位;

2. 若字典中没有与输入相同的单词,认为输入单词错误,需要从字典中选择一个修正单词;

3. 修正要求:与输入单词长度相同,且单词中不同字符数最少;

4. 存在多个修正单词时,取字典中的第一个;

5. 输出修正后的单词。

 

  • 要求实现函数:

void FixWord(const char *pInputWord, long lWordLen, const char pWordsDic[][MAX_WORD_LEN], long lDicLen, char *pOutputWord);

【输入】pInputWord:  输入的单词

        lWordLen:    单词长度

        pWordsDic:   字典单词数组

        lDicLen:     字典单词个数

【输出】 pOutputWord: 输出的单词,空间已经开辟好,与输入单词等长

【注意】不考虑空单词和找不到长度相同的单词等异常情况。

  • 示例:

输入:“goad”

字典:“god good wood”

输出:“good”

 

#include "stdafx.h"
#include <iostream>
using namespace std;


const char input[]="good";
const int MAX_WORD_LEN=9;
const int Dic_len=3;
const int input_len=4;
const char pWordsDic[Dic_len][MAX_WORD_LEN]={"god","god","wod"};
//void FixWord(const char *pInputWord,long lWordLen,const char pWordsDic[][MAX_WORD_LEN],long lDicLen,char *pOutputWord)
//{
//
//}
int _tmain(int argc, _TCHAR* argv[])
{	
	int *all_len=new int [Dic_len];
	for(int i=0;i<Dic_len;++i)
	{
		all_len[i]=strlen(pWordsDic[i]);		
	}
	int diffrent=0;
	int temp=input_len;
	int count=0;
	for(int i=0;i<Dic_len;++i)
	{
		diffrent=0;
		if(input_len!=all_len[i])
			continue;
		for(int j=0;j<input_len;++j)
		{
			if(input[j]!=pWordsDic[i][j])
				diffrent++;
			if(diffrent<temp)
			{
				temp=diffrent;
				count=i;
			}
		}
	}
	cout<<pWordsDic[count];
	return 0;
}

  

转载于:https://www.cnblogs.com/xd-jinjian/p/3277220.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值