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

golf z func #229

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

lrvideckis
Copy link
Contributor

The idea behind this change is:

each iteration of the while loop will increase the right border  
$r$  of the match segment

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 true

here's an AC to show it's still O(n) https://codeforces.com/contest/126/submission/216604260

@lrvideckis
Copy link
Contributor Author

@bjorn-martinsson
Copy link
Contributor

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 z is non-negative, so removing the = doesn't make any difference.

@lrvideckis
Copy link
Contributor Author

lrvideckis commented Aug 1, 2023

@IvanRenison
Copy link
Contributor

IvanRenison commented Apr 2, 2024

@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)

@bjorn-martinsson
Copy link
Contributor

@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?

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

Successfully merging this pull request may close these issues.

3 participants