【发布时间】:2021-10-27 21:09:21
【问题描述】:
问题:
给定一个包含 N 个字符的字符串 S (N <= 200 000),求出现至少两次的最长子字符串的长度(子字符串可以重叠)。
我的解决方案:
这是我尝试过的:
int main()
{
std::string s;
std::cin >> s;
int max = 0;
typedef std::string::const_iterator sit;
sit end = s.end();
for(sit it1 = s.begin(); it1 != end; ++it1)
for(sit it2 = it1 + 1; it2 != end; ++it2)
max = std::max(max, std::mismatch(it1, it1 + (end - it2), it2).first - it1);
std::cout << max;
}
问题:
但是,上述解决方案将在 O(n^3) 中运行时获得 TLE。有什么方法可以改进它,使它可以在 O(n.logn) 中运行?
【问题讨论】:
-
“因为它在
O(n^2)中运行”。是O(n^3),你忘了数std::mismatch。
标签: c++ string algorithm substring longest-substring