summaryrefslogtreecommitdiff
path: root/string/z.cpp
diff options
context:
space:
mode:
authorNoobie99 <noob999noob999@gmail.com>2023-09-15 17:07:23 +0200
committerNoobie99 <noob999noob999@gmail.com>2023-09-15 17:07:23 +0200
commit177e2de6ed58fe3fa930c19a097eaf38e31b7043 (patch)
tree501e4ded1d03fe4578a15e8a771ac0ad437997ac /string/z.cpp
parentcc86ae3dcc48e187d626584b22f66ab6906a9564 (diff)
slightly shorten z function
Diffstat (limited to 'string/z.cpp')
-rw-r--r--string/z.cpp9
1 files changed, 4 insertions, 5 deletions
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<int> Z(const string& s) {
int n = sz(s);
- vector<int> 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<int> 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;
}