[ 登录注册 ]

C++

雅虎笔试题(字符串操作)

2016/05/13 09:34 [db:作者] 返回上一页

给定字符串A和B,输出A和B中的最大公共子串。
比如A=”aocdfe” B=”pmcdfa” 则输出”cdf”
*/
//Author: azhen
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *commanstring(char shortstring[], char longstring[])
{
int i, j;
char *substring=malloc(256);
if(strstr(longstring, shortstring)!=NULL)????????????? //如果……,那么返回shortstring
return shortstring;
for(i=strlen(shortstring)-1;i>0; i–)???????????????? //否则,开始循环计算
{
for(j=0; j<=strlen(shortstring)-i; j++){
memcpy(substring, &shortstring[j], i);
substring[i]=’\0′;
if(strstr(longstring, substring)!=NULL)
return substring;
}
}
return NULL;
}

main()
{
char *str1=malloc(256);
char *str2=malloc(256);
char *comman=NULL;
gets(str1);
gets(str2);
if(strlen(str1)>strlen(str2))???????????????????????? //将短的字符串放前面
comman=commanstring(str2, str1);
else
comman=commanstring(str1, str2);
printf(“the longest comman string is: %s\n”, comman);
}


文章来源:http://www.bozhiyue.com/mianshiti/CPP/2016/0513/95764.html
评论
发表评论