匹配输入华为:编程实现联想输入法 输入联想功能是非常实用的一个功能,请编程实现类似功能
首先声明,我是一个菜鸟。一下文章中出现技术误导情况盖不负责
/**************************************************************************** 输入想联 问题描述: 输入想联功能是非常用实的一个功能,请编程实现相似功能。 求要实现函数: 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<cstdlib> #include<cstring> #include<cstdio> #include <iostream> using namespace std; const int N=256; 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 //匹配失败 直接跳出本次whil循环 停止下一个单词的匹配 { break; } } if(tmp[j] == '\0') //带匹配单词结束 匹配胜利 { for(k=0;k<i;++k) output[cnt++]=word[k]; //将匹配胜利的单词存保到结果数组 output[cnt++]=' '; //匹配胜利的单词之间以空格开隔 } memset(word,0,i); //word内容已被存保,清零预备下一个单词的匹配 i=0; //i是描扫变量,描扫结束的值是本次匹配单词的度长 *str++; //这是最外围循环结束的渐变量 } } output[cnt]='\0'; //添加字符串结束志标 } int main() { char in[]= " UESTC EE chengdu china bejing zhengzhou henan uestc ee wangshihui huawei \ To recap, the three main objectives in the Mystery Method are: \ To attract a woman \ To establish comfort, trust, and connection \ To structure the opportunity to be seduced "; char tmp[10]={0}; char out[255]={0}; gets(tmp); while(strcmp(tmp,"0")!=0) { auto_complete(in,tmp,out); cout<<"out is "<<out<<endl; memset(out,0,sizeof(out)); gets(tmp); } } /************************************ c out is chengdu china comfort, connection e out is ee establish T out is To To To To w out is wangshihui woman a out is are: attract a and b out is bejing be c out is chengdu china comfort, connection d out is e out is ee establish M out is Mystery Method 0 re out is recap, tru out is trust, to out is to in out is in he out is henan wa out is wangshihui h out is henan huawei o out is objectives opportunity wo out is woman att out is attract 0 Process returned 0 (0x0) execution time : 48.348 s Press any key to continue. Process returned 0 (0x0) execution time : 39.858 s Press any key to continue. *************************************/
文章结束给大家分享下程序员的一些笑话语录: IBM和波音777
波音777是有史以来第一架完全在电脑虚拟现实中设计制造的飞机,所用的设备完全由IBM公司所提供。试飞前,波音公司的总裁非常热情的邀请IBM的技术主管去参加试飞,可那位主管却说道:“啊,非常荣幸,可惜那天是我妻子的生日,So..”..
波音公司的总载一听就生气了:“胆小鬼,我还没告诉你试飞的日期呢!”