-
getZ232's competitive-programming templates. 2024. 7. 26. 08:49
template <typename C, typename Cmp = std::equal_to<>> std::vector<int> getZ(const C& cont, Cmp&& cmp = Cmp {}) { assert(!cont.empty() && "at function \"getZ\""); int n = int(cont.size()); std::vector<int> z(n); int opt = 0; for (int i = 1; i < n; ++i) { if (opt + z[opt] > i) { z[i] = std::min(opt + z[opt] - i, z[i - opt]); } while (i + z[i] < n && cmp(cont[z[i]], cont[i + z[i]])) { z[i] += 1; } if (opt + z[opt] < i + z[i]) { opt = i; } } z[0] = n; return z; }
'232's competitive-programming templates.' 카테고리의 다른 글
Combinations (0) 2024.08.01 ModInt (0) 2024.08.01 Fail function and Linear string search (0) 2024.07.26 Fenwick (0) 2024.07.25 UnionFind (1) 2024.07.25