XDJM帮做下一道算法题

多么痛的领悟 2010-11-22 11:37:55
RT,用c或者c++实现
两个字符串比较,求出最大的公共子串的算法
小弟感谢了
...全文
74 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
科技完美生活 2010-11-23
  • 打赏
  • 举报
回复
网上搜吧,肯定有
MSOKD 2010-11-23
  • 打赏
  • 举报
回复
strcmp()
luciferisnotsatan 2010-11-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jixingzhong 的回复:]

把问题延伸一下:
http://blog.163.com/mamimoluo@126/blog/static/3890759320090710749417/
[/Quote]
收藏了
swlilike 2010-11-23
  • 打赏
  • 举报
回复
知道大牛为啥得这么多奖章了
hk2305621_1 2010-11-23
  • 打赏
  • 举报
回复
KMP算法
jixingzhong 2010-11-23
  • 打赏
  • 举报
回复
把问题延伸一下:
http://blog.163.com/mamimoluo@126/blog/static/3890759320090710749417/
jixingzhong 2010-11-23
  • 打赏
  • 举报
回复
参考:
#include<iostream>

  #include<cstring>

  #include<cassert>

  using namespace std;

  
  //找出两个字符串中最长的公共子串(如果存在多个 , 返回第一个)

  //参数:str1   字符串1

  //   str2 字符串2

  //   maxSubStr    找到的最大子串

  void findMaxSubstr(const char * str1 , const char * str2 , char * maxSubstr){

  assert((str1!=NULL)&&(str2!=NULL));

  assert(maxSubstr!=NULL);

  int maxPos=-1;

  int maxLen=0;

  for(int i=0; i<strlen(str1); i++){

    for(int j=0; j<strlen(str2); j++){

      if(str1[i]==str2[j]){

        for(int k=1; (str1[i+k]==str2[j+k])&&(str1[i+k]!='\0'); k++);

        if(k>maxLen){

          maxPos=i;

          maxLen=k;

        }

      }

    }

  }

  

  if(maxPos==-1){

    maxSubstr[0]='\0';

  }else{

    memcpy(maxSubstr , str1+maxPos , maxLen);

    maxSubstr[maxLen]='\0';

  }

}



int main(){

  char substr[20];

  findMaxSubstr("zhangligu" , "gligzhangligu" , substr);

  cout<<substr<<endl;

  return 0;

}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧