基于visual Studio2013解决面试题之1002公共子串




题目



解决代码及点评

/*
	求两个字符串的最小公共子串

	这道题简单的两个字符串遍历即可
*/




#include <iostream>   
using namespace std;  
const int N = 1000;  
  
char* go(char *str1,char *str2)  
{  
    int maxindex;   
    int maxlength = 0;  
    int i,j;  
    for(i=0;str1[i];i++)  
    {  
        for(j=0;str2[j];j++)  
        {  
            for(int k=0;str1[i+k]==str2[j+k] && (str1[i+k] || str2[i+k]);k++)  
                if(k>maxlength)  
                {  
                    maxindex = j;  
                    maxlength = k+1;  
                }  
        }  
    }  
      
    char *result = new char[maxlength+1];  
    for(i=0;i<maxlength;i++)  
        result[i] = str2[maxindex++];  
    result[i] = '/0';  
    return result;  
   
}  
  
int main()  
{  
  

	char *str1 = "abractyeyt"; 
    char *str2 = "dgdsaeactyey"; 
    
    cout<<go(str1,str2)<<endl;
	system("pause");
    return 0;  
}  



代码下载及其运行

代码下载地址:http://download.csdn.net/detail/yincheng01/6704519

解压密码:c.itcast.cn


下载代码并解压后,用VC2013打开interview.sln,并设置对应的启动项目后,点击运行即可,具体步骤如下:

1)设置启动项目:右键点击解决方案,在弹出菜单中选择“设置启动项目”


2)在下拉框中选择相应项目,项目名和博客编号一致

3)点击“本地Windows调试器”运行


程序运行结果








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尹成

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值