-
Notifications
You must be signed in to change notification settings - Fork 748
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
golf z func #229
base: main
Are you sure you want to change the base?
golf z func #229
Conversation
btw, the same idea can be used here https://github.com/kth-competitive-programming/kactl/blob/main/content/strings/Manacher.h#L19-L20 |
On the subject on codegolfing the z function. You can save one more character by changing z[i] = i >= r ? 0 : min(r - i, z[i - l]); into z[i] = i > r ? 0 : min(r - i, z[i - l]); This works since |
@lrvideckis I tested this version o Zfunc and the current one, and in the problem in witch I tested it it was faster with the current one than with this one. Did you made a more exhaustive test about speed? (I only tested it in one problem, so it may not be something general) |
Could you link to your submissions comparing the two? |
Current versión: https://codeforces.com/gym/105053/submission/254710440 Files in github: |
The idea behind this change is:
https://cp-algorithms.com/string/z-function.html#asymptotic-behavior-of-the-algorithm
So basically if we successfully enter in the while loop, then we know the condition
(i + z[i] > r)
is truehere's an AC to show it's still O(n) https://codeforces.com/contest/126/submission/216604260