-
Notifications
You must be signed in to change notification settings - Fork 7.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rk算法for循环是两次,时间复杂度为何是n呢 #514
Comments
|
在 Rabin-Karp (RK) 字符串匹配算法中,时间复杂度的分析重点在于如何计算每一轮的哈希值。你给出的代码中有两层 for 循环,但其中的内层循环(第二个 for)在优化计算哈希值时可以被看作常数时间复杂度。 内层循环的优化与哈希计算 但是在你提供的代码中,内层循环每次都会从头重新计算一遍哈希值,这样的时间复杂度确实会导致 O(m * n) 的复杂度。 如果使用滚动哈希技巧,内层循环 for (j = 0; j < n; j++) 可以在 O(1) 时间内完成每个新的哈希值更新。因此,优化后代码的时间复杂度是 O(m),而不是 O(m * n)。 优化后的代码: public class Main {
} |
No description provided.
The text was updated successfully, but these errors were encountered: