问题:求两个字符串的最长公共子串。 要求:输入两个字符串,输出它们的最长公共子串,包括长度。

#include <stdio.h>
#include<stdlib.h>
#include<string.h>
/*
问题:求两个字符串的最长公共子串。
要求:输入两个字符串,输出它们的最长公共子串,包括长度。
设计:
设计一个类 String,包括一个 len(字符串长度)和字符串指针 s。另有如下成员函数:
· void getstring() 从用户获取一个字符串。
· void display() 输出字符串。
· friend String maxsubstring(String str1,String str2) 友元函数,求两个字符串的最长
公共子串
*/
void main()
{
char *s1 = "woaibeijingtiananmen";
char *s2 = "welcometobeijing";
int count[50] = { 0 };
int i = 0;
int k = 0;
int max = 0;
int m = 0;
char *ps[50] = { 0 };




for (char *pc1 = s1; *pc1 != '\0'; pc1++)
{
for (char *pc2 = s2; *pc2 != '\0'; pc2++)
{
char *temp1 = pc1;
char *temp2 = pc2;
if ((*temp1 == *temp2) && (*temp2 != '\0'))
{
   ps[i] = temp1;//保留地址
while ((*temp1 == *temp2) && (*temp2 != '\0'))
{
temp1++;
temp2++;
count[i]++;
}
i++;
}
}
}


for (int i = 0; i < 50; i++)
{
//printf("%d\n", count[i]);
if (max < count[i])
{
k = i;
max = count[i];




}
}
printf("字符串子串最大长度:%d\n", max);
printf("该子串为:");
/*for (int i = k;m<max; )
{
printf("     %c ", *ps[i]);
ps[i]++;
m++;
}*/
while (m < max)
{
printf("     %c ", *ps[k]);
ps[k]++;
m++;
}






printf("\n");
system("pause");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值