c语言 求大佬
输出两个字符串的最长公共子串例如:
输入:
str1[]="123zxc456"
str2[]="123789z"
则输出:
123
求求大佬了。 随便写了个,算法不优,将就着试试吧
#include "stdio.h"
void _GetMaxSubString(char * s1,char * s2)
{
int _begin = 0;
int _tempBegin = 0;
int _len = 0;
int _max = 0;
int i = 0;
int j = 0;
int flag = 0;
if(s1==0 || s2==0)
{
return;
}
while(s1 != 0)
{
while(s2 != 0)
{
if(s1 != 0 && s1 == s2)
{
if(flag == 0)
{
_tempBegin = i;
flag = 1;
}
_max++;
}
else
{
if(flag && _max > _len)
{
_len = _max;
_begin = _tempBegin;
}
flag = 0;
_max = 0;
}
j++;
}
j=0;
i++;
}
for(i=0 ; i <_len ; i++)
{
printf("%c",s1);
}
}
int main()
{
_GetMaxSubString("Hello,Hello,wo","good luck,Hello,world");
return 0;
} 谢谢大佬 #include<stdio.h>
#include<stdlib.h>
int main()
{
char str1, str2, str3;
int x, y, a = 0, b = 0;
int m = 0, n = 0;
for (x = 0; x < 10; x++)
{
scanf("%c", &str1);
}
getchar();
for (x = 0; x < 10; x++)
{
scanf("%c", &str2);
}
getchar();
for (x = 0; x < 10; x++)
for (y = 0; y < 10; y++)
str3 = '0';
for (x = 0; x < 10; x++)
for (y = 0; y < 10; y++)
if (str1 == str2)
{
str3 = str1;
while (x != 9 && y != 9 && str1[++x] == str2[++y])
{
b++;
str3 = str1;
if (str1 != str2)
x--;
}
a++;
b = 0;
}
for (x = 0; x < 9; x++)
{
for (y = 0; y < 9; y++)
if (str3 != '0')
m++;
if (m>n)
{
n = m;
m = 0;
a = x;
}
}
for (x = 0; x < 9; x++)
{
if (str3 != '0')
{
printf("%c", str3);
}
if (str3 == '0')
{
printf("没有相同的字符串");
break;
}
}
system("pause");
}
页:
[1]