Skip to content
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

我认为01.05.md中最长回文子串中Manacher算法时间复杂度不是O(N),应是O(N^2) #385

Open
hongsenliu opened this issue Oct 20, 2014 · 2 comments

Comments

@hongsenliu
Copy link

我认为01.05.md中最长回文子串中Manacher算法时间复杂度不是O(N),应是O(N^2).
N是原始输入string的长度,加了分隔符#之后是2N+1,再加$符号就是2N+2.
例如:s="1234321". N=7, 加了两种分隔符后是2N+2=16, s1="$#1#2#3#4#3#2#1#"
外层的for loop, 当i=8,里面的while loop要循环7次。 所以我认为时间复杂度是O(N^2).
请勘校。

@ZumiKua
Copy link

ZumiKua commented Sep 13, 2016

当一个回文串的右边界超过mx以后,mx会被更新至该右边界,而后续的while loop均会在mx之后进行查找。实际上,当一个字符被while loop访问以后,mx会被更新至该字符之后,进而所有的while loop会在该字符之后进行查找,所以一个字符仅会被while loop访问一次(仅指通过s[i + p[i]]访问的部分),故该算法的时间复杂度为O(n)。如果不理解的话,也可以自己写个程序测试一下while循环执行的次数,相信你会弄懂的;)

@ArtixZ
Copy link

ArtixZ commented Aug 14, 2019

mx 之外的字符 不会被更新至字符之后. 我认为应该是会重复访问字符的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants