找出两个字符串中最大公共子字符串,如"abccade"、"dgcadde"的最大子串为"cad"
2014-06-23 22:49
661 查看
//// 此题用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;
}
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;
}
相关文章推荐
- 找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为"cad"
- 135、编程实现:找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为 "cad"
- 找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为"cad"
- 找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为"cad"
- 找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为"cad"
- 找出两个字符串中最大子字符串,如"abractyeyt","dgdsaeactyey"的最大子串为"actyet
- 一个字符串A的子串被定义成从A中顺次选出若干个字符构成的串。如A=“cdaad" ,顺次选1,3,5个字符就构成子串" cad" ,现给定两个字符串,求它们的最长共公子串。 小王对既是素数又是回文的
- 一个字符串A的子串被定义成从A中顺次选出若干个字符构成的串。如A=“cdaad" ,顺次选1,3,5个字符就构成子串" cad" ,现给定两个字符串,求它们的最长共公子串。 Input 第一行两个字
- 找出两个字符串中最大的公共子串(java实现)
- java基础-找出两个字符串相同的最大子串
- 阿里巴巴2014.08.29校园招聘笔试题---找出两个字符串中连续相同的最大子串
- Java找出两个字符串中最大的子串
- 找出两个字符串中最大的公共子字符串
- 找出两个字符串的最大公共子串
- 找出两个字符串中最大的公共子串的简单实现
- 从两个字符串中找出最大公共子字符串
- 找出两个字符串的最大公共子串
- 找出两个字符串的最大公共子串
- 给一个很长的字符串str 还有一个字符集比如"abc" 找出str 里包含"abc"的最短子串。要求O(n)
- 找出包含字符串"from"但不含字符串"127.0.0.1"的txt文件 (用SQL+Batch+grep求两个文件集合的差.)