求两个字符串公共子序列的最大长度(参考阿里巴巴2015研发笔试)(简单)

题目:给定字符串s1,s2,均由小写字母祖闯,要求在s1中找出以同样顺序连续出现在s2中的最长连续序列的长度;如s1=“acaccbabb”,s2 = “acbac”,那么s1,s2的最大公共子串为“cba”,对应的长度为3。

思路:这里采用暴力匹配方法,时间复杂度为O(mn)

具体代码如下:

#include<stdio.h>
#include<string.h>
//寻找两个字符串最长公共子序列的长度 
int bfMatch(char *a,char *b,int max){
	int result,i,j,itemp;
	max = 0;
	for(i=0;a[i]!='\0';i++){
		itemp = i;
		result = 0;
		for(j=0;b[j]!='\0';j++){
			if(a[itemp] == b[j]){
				result ++;
				if(max < result)
					max = result;
				itemp ++;
			}
			else{
				result = 0;//结果置0 ; 
				itemp = i;//itemp回溯至i,从j的当前位置开始搜索; 
			}
		}
		if(itemp == strlen(a))//若itemp已搜索至a串末尾 
			break;
	}
	return max;
} 

int main(){
	char b[] = "acbac";
	char a[] = "acaccbabb";
	printf("%d\n",bfMatch(a,b,0));
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值