2012届华为编程大赛第二题

/*
华为2012年编程大赛
2. 输入联想(30分)
问题描述: 
输入联想功能是非常实用的一个功能,请编程实现类似功能。
要求实现函数: 
void auto_complete(char *str, char *tmp,char *output)
【输入】  char *str,候选字符串
char *tmp,输入字符串
【输出】  int *output,联想匹配的字符串
【返回】  无
注:候选字符串以空格隔开,输入字符串仅从字符串开始处匹配。将匹配的子字符串输出,同样以空格隔开。如无匹配成功的子字符串,则输出空字符串。
示例 
输入:str = chengdu chongqing,tmp = c
输出:output = chengdu Chongqing

输入:str = chengdu chongqing,tmp = che
输出:end = Chengdu

3)输入:str = beijing nanjing,tmp = jing
输出:end =  

*/

#include "stdafx.h"
#include "iostream"

using namespace std;

void auto_complete(char *str, char *tmp,char *output)
{
	int str_position = 0;
	int tmp_position = 0;
	int out_position = 0;
	while (str[str_position] != '\0')
	{
		// 相同时候,自加
		if (tmp[tmp_position] == str[str_position])
		{
			tmp_position++;
			str_position++;
			// 完全相同,复制
			if (tmp[tmp_position] == '\0')
			{
				str_position = str_position - tmp_position;
				while (str[str_position] != ' ' && str[str_position] != '\0')
				{
					output[out_position] = str[str_position];
					out_position ++;
					str_position ++;
				}
				output[out_position++] = ' ';
				while (str[str_position] == ' ' && str[str_position] != '\0')
				{
					str_position++;
				}
				tmp_position = 0;
			}
		}
		// 不同时候,str_position自加,tmp_position赋0
		else 
		{
			tmp_position = 0;
			while (str[str_position] != ' ' && str[str_position] != '\0')
			{
				str_position++;
				
			}
			while (str[str_position] == ' ' && str[str_position] != '\0')
			{
				str_position++;
			}
		}
	}
	output[out_position - 1] = '\0';
	return;
	
	
}

int _tmain(int argc, _TCHAR* argv[])
{
	char str[500];
	cout << "请输入候选字符串:";
	cin.getline(str, 500);
	char tmp[500];
	cout << "请输入字符串:";
	cin >> tmp;
	char output[500];
	auto_complete(str, tmp, output);
	cout << "联想匹配的字符串:" << output << endl;
	system("pause");
	return 0;
}


做的时候逻辑混乱了,没看清题就开始做。

后面实在不行画了个图,稍好了点

思路如下




另外转自http://www.cnblogs.com/ballwql/archive/2013/04/18/3028709.html的代码

同样可行。与我代码的不同之处在于其逻辑较为清晰,将每个词语取出,分别进行比较运算。

void auto_complete(char *str,char *tmp, char *output)
{
    char word[N];
    memset(word,0,sizeof(word));
    int i=0,k=0,j=0,cnt=0;
    int len=strlen(str);
    if(!strlen(tmp))
        return;
    while(*str)
    {
        if(*str != ' ')
        {
            word[i++]=*str++;
            
        }
        else
        {
            k=0;j=0;
            
            while(k<i && tmp[j] != '\0')
            {
                if(word[k]==tmp[j])
                {
                    k++; 
                    j++;
                }
                else
                {
                    break;
                }                    
            }
            if(tmp[j] == '\0')            
            {
                for(k=0;k<i;++k)
                    output[cnt++]=word[k];
                output[cnt++]=' ';
                
            }
            memset(word,0,i);
            i=0;
            *str++;
        }        
    }
    k=0;j=0;
            
    while(k<i && tmp[j] != '\0')
    {
        if(word[k]==tmp[j])
        {
            k++; 
            j++;
        }
        else
        {
            break;
        }                    
    }
    if(tmp[j] == '\0')            
    {
        for(k=0;k<i;++k)
            output[cnt++]=word[k];        
        
    }
    output[cnt]='\0';
}


以下这个可行的,同样转自刚那个链接,需要头文件sstream

void auto_complete(char *str,char *tmp, char *output)
{
    istringstream istream((string)(str));
    string word;
    int i=0,j=0,k=0;
    while(istream>>word)
    {
        
        int pos=word.find((string)(tmp));
        cout<<pos<<endl;
        if(pos == 0)
        {
            for(;pos!=word.size();++pos)
                output[k++]=word[pos];
            output[k++]=' ';
        }
        
    }
    output[--k]='\0';
    
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值