给定一个 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");
}