找出两个字符串中最大公共子字符串,如"abccade"、"dgcadde"的最大子串为"cad"

此题用for能控制循环
int GetCommon(char *s1, char *s2, char **r1, char **r2) 

 int len1 = strlen(s1); 
 int len2 = strlen(s2); 
 int maxlen = 0; 
 for(int i = 0; i < len1; i++) 
 {
  for(int j = 0; j < len2; j++) 
  { 
   if(s1[i] == s2[j]) //找到了第一个相等的 
   { 
    int as = i, bs = j, count = 1; // 保存第一个相等的首地址 
    while(as + 1 < len1 && bs + 1 < len2 && s1[++as] == s2[++bs]) //查找最大相等长度 
     count++; 
    if(count > maxlen) //如果大于最大长度则更新 
    { 
     maxlen = count; 
     *r1 = s1 + i; 
     *r2 = s2 + j; 
    }
  }
 }
}


下面是我的程序:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
char *find_common(char *str1, char *str2)
{
     int as,bs,i = 0,j = 0;//记录字符移动位置
     int maxlen = 0,count; //记录最大公共子串的长度
     int len1 = 0,len2 = 0;//保存待测字符的长度
     char *string;//保存最大公共子串
     while(*(str1 + len1) != '\0')
     {
          len1++;
     }
     while(*(str2 + len2) != '\0')
     {
          len2++;
     }
     string = (char *)malloc(len1 > len2 ? len1 : len2);//动态分配内存
     for(i=0; i<len1; i++)
          for(j=0; j<len2; j++)
          {
               if(*(str1 + i) == *(str2 + j))
               {
                    as = i;
            bs = j;
    count = 1;
    while(as + 1 < len1 && bs + 1 < len2 && str1[++as] == str2[++bs]) //查找最大相等长度 
     count++;

    if(count > maxlen)
    {
     maxlen = count;
     strncpy(string, str1+i, maxlen);
    }   
   } 
 *(string + maxlen) = '\0'; 
}
 return string;

}

int main()
{
 char *str1 = NULL;
 char *str2 = NULL;
 char *maxstring;
 str1 =(char *)malloc(256);
 str2 =(char *)malloc(256);
 printf("Please input the first string:");
 scanf("%s",str1);
 printf("Please input the second string:");
 scanf("%s",str2);
 maxstring = find_common(str1, str2);
 printf("The maxlen is: %s\n",maxstring);
 free(maxstring);
 free(str2);
 free(str1);
 return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!下面是一个使用C语言编写的程序,用于找出两个字符串最大公共子串: ```c #include <stdio.h> #include <string.h> void findLCS(char *str1, char *str2) { int len1 = strlen(str1); int len2 = strlen(str2); int maxLen = 0; // 最大公共子串的长度 int endIdx = 0; // 最大公共子串的结束索引 // 创建一个二维数组用于存储最长公共子串的长度 int dp[len1 + 1][len2 + 1]; // 初始化dp数组 for (int i = 0; i <= len1; i++) { for (int j = 0; j <= len2; j++) { if (i == 0 || j == 0) dp[i][j] = 0; else if (str1[i - 1] == str2[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; if (dp[i][j] > maxLen) { maxLen = dp[i][j]; endIdx = i - 1; } } else { dp[i][j] = 0; } } } // 打印最大公共子串 if (maxLen == 0) { printf("No common substring found.\n"); } else { printf("Longest common substring: "); for (int i = endIdx - maxLen + 1; i <= endIdx; i++) { printf("%c", str1[i]); } printf("\n"); } } int main() { char str1[100]; char str2[100]; printf("Enter the first string: "); fgets(str1, sizeof(str1), stdin); str1[strcspn(str1, "\n")] = '\0'; // 移除输入字符串的换行符 printf("Enter the second string: "); fgets(str2, sizeof(str2), stdin); str2[strcspn(str2, "\n")] = '\0'; // 移除输入字符串的换行符 findLCS(str1, str2); return 0; } ``` 你可以使用上述代码来找出两个字符串最大公共子串。程序会要求你输入两个字符串,然后打印出最大公共子串。请注意,程序使用了一个二维数组 `dp` 来存储最长公共子串的长度,使用动态规划的思想进行求解。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值