From 177e2de6ed58fe3fa930c19a097eaf38e31b7043 Mon Sep 17 00:00:00 2001 From: Noobie99 Date: Fri, 15 Sep 2023 17:07:23 +0200 Subject: slightly shorten z function --- string/z.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'string/z.cpp') diff --git a/string/z.cpp b/string/z.cpp index f3824f3..c128e9d 100644 --- a/string/z.cpp +++ b/string/z.cpp @@ -1,11 +1,10 @@ vector Z(const string& s) { int n = sz(s); - vector z(n, n); - int x = 0, y = 0; - for (int i = 1; i < n; i++) { - z[i] = max(0, min(z[i - x], y - i + 1)); + vector z(n); + for (int i = 1, x = 0; i < n; i++) { + z[i] = max(0, min(z[i - x], x + z[x] - i)); while (i + z[i] < n && s[z[i]] == s[i + z[i]]) { - x = i, y = i + z[i], z[i]++; + x = i, z[i]++; }} return z; } -- cgit v1.2.3