请帮我寻找最长公共子串问题实现的运行时间。
int main(){
string a;
string b;
cin>>a>>b;
string::iterator a1,b1;
string max,temp;
for(a1=a.begin();a1!=a.end();a1++){
b1=find(b.begin(),b.end(),*a1);
if(b1!=b.end()){
temp+=(*b1);
while( ((b1+1) != (b.end())) and ((*(a1+1))==(*(b1+1)))){
a1++;
b1++;
temp+=(*b1);
}
if(max.size()<temp.size()){
max.assign(temp);
}
temp.clear();
}
}
cout<<max;
}
函数 std::find 的时间复杂度是 O(n),这意味着对于两个字符串长度分别为 n 和 m,使用该函数的时间复杂度应为 O(nm)。但是,它是否会比 O(nm) 更高?
std::sort
的时间复杂度为 O(n log n)(大多数标准排序算法的实现也是如此)。此外,还要记住不能忽略find
步骤所需的时间,其时间复杂度为 O(m)。基于这些,你认为运行时间是多少? - templatetypedefO(n.m)
。 - Jarod42a = "abbbbbba" 和 b = "ababbbbbba"
进行测试,你的结果是错误的。 - Jarod42