aboutsummaryrefslogtreecommitdiff
path: root/08_string_algorithms/finding_periods_1733.cpp
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2026-07-06 19:08:08 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2026-07-06 19:08:08 +0200
commit96254947699986c59f0dc63d69fd4b76bd3ed43e (patch)
tree6c4dca945d7f7427c48be234d827fe4d33be02c5 /08_string_algorithms/finding_periods_1733.cpp
downloadcses-96254947699986c59f0dc63d69fd4b76bd3ed43e.tar.gz
cses-96254947699986c59f0dc63d69fd4b76bd3ed43e.zip
Initial commit
Diffstat (limited to '')
-rw-r--r--08_string_algorithms/finding_periods_1733.cpp29
1 files changed, 29 insertions, 0 deletions
diff --git a/08_string_algorithms/finding_periods_1733.cpp b/08_string_algorithms/finding_periods_1733.cpp
new file mode 100644
index 0000000..edaa1d1
--- /dev/null
+++ b/08_string_algorithms/finding_periods_1733.cpp
@@ -0,0 +1,29 @@
1#include <algorithm>
2#include <iostream>
3#include <string>
4#include <vector>
5
6bool isp(const std::vector<size_t>& z, size_t k) {
7 for (size_t i = 0; i*k < z.size(); i++)
8 if (z[i*k] < std::min(k, z.size()-i*k))
9 return false;
10 return true;
11}
12
13int main() {
14 std::string s;
15 std::cin >> s;
16 std::vector<size_t> z(s.size(), 0);
17 z[0] = s.size();
18 for (size_t i = 1, j = 0, k = 0; i < s.size(); i++) {
19 if (j < i || z[i-k] == j-i) {
20 for (j = std::max(i, j); j < s.size() && s[j] == s[j-i]; j++) ;
21 z[k=i] = j-i;
22 } else z[i] = std::min(z[i-k], j-i);
23 }
24
25 for (size_t i = 1; i <= s.size(); i++)
26 if (isp(z, i))
27 std::cout << i << " ";
28 std::cout << "\n";
29}

Generated with cgit - Back to sebastiano.tronto.net