最长公共子串的长度(阿里)

给定一个 query 和一个 text,均由小写字母组成。要求在 text 中找出以同样的顺序连 续出现在 query 中的最长连续字母序列的长度。例如, query 为“acbac”,text 为 “acaccbabb”,那么 text 中的“cba”为最长的连续出现在 query 中的字母序列,因此, 返回结果应该为其长度 3。请注意程序效率。

#include<iostream> 
#include<string>
using namespace std;
int max(string text, string query){
	string temp;
	int len1 = text.length();
	int len2 = query.length();
	int i, j;
	for (i = len2; i > 0; i--){ //i表示最长的公共子串
		for (j = 0; j + i <= len2; j++){
			temp = query.substr(j, i);  //子串
			if (text.find(temp) != text.npos)//nops用来表示不存在的意思,这句的意思是在text中找到了temp
				return i;
		}
	}
	return 0;
}

void main(){
	string text, query;
	cin >> text >> query;
	int l = max(text, query);
	if (l == 0)
		cout << "not match"<<endl;
	else
		cout << l << endl;
	
	
	system("pause");
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值