找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为"cad"
2010-09-26 09:12
656 查看
#include <iostream.h>
#include <string>
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;
}
}
}
}
return maxlen;
}
void main()
{
char *r1,*r2;
char *str1="nbasdfghjkl",*str2="asdfgbhjnmk";
cout<<GetCommon(str1,str2,&r1,&r2);
for(int i=0;i<GetCommon(str1,str2,&r1,&r2);i++)
cout<<*(r2+i);
}
#include <string>
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;
}
}
}
}
return maxlen;
}
void main()
{
char *r1,*r2;
char *str1="nbasdfghjkl",*str2="asdfgbhjnmk";
cout<<GetCommon(str1,str2,&r1,&r2);
for(int i=0;i<GetCommon(str1,str2,&r1,&r2);i++)
cout<<*(r2+i);
}
相关文章推荐
- 找出两个字符串中最大公共子字符串,如"abccade"、"dgcadde"的最大子串为"cad"
- 找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为"cad"
- 找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为"cad"
- 找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为"cad"
- 135、编程实现:找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为 "cad"
- 一个字符串A的子串被定义成从A中顺次选出若干个字符构成的串。如A=“cdaad" ,顺次选1,3,5个字符就构成子串" cad" ,现给定两个字符串,求它们的最长共公子串。 小王对既是素数又是回文的
- 找出两个字符串中最大子字符串,如"abractyeyt","dgdsaeactyey"的最大子串为"actyet
- 一个字符串A的子串被定义成从A中顺次选出若干个字符构成的串。如A=“cdaad" ,顺次选1,3,5个字符就构成子串" cad" ,现给定两个字符串,求它们的最长共公子串。 Input 第一行两个字
- 阿里巴巴2014.08.29校园招聘笔试题---找出两个字符串中连续相同的最大子串
- Java找出两个字符串中最大的子串
- 编程实现查找两个字符串的最大公共子串 示例:"aocdfe"和"pmcdfa"最大公共子串为"cfd"
- 找出多个字符串中的最大公共子字符串,如“nbitheimanb”和“itheia”的最大子串是:”ithei”。
- 找出两个字符串的最大公共子串
- 找出两个字符串的最大公共子串
- 从两个字符串中找出最大公共子字符串
- (1) 找出两个字符串的最大子串
- java基础—找出两个字符串中最大的子串
- 找出两个字符串中最大的公共子串(java实现)
- 找出两个字符串中最大的公共子串的简单实现
- 找出两个字符串中最大的公共子串的简单实现