aboutsummaryrefslogtreecommitdiff
path: root/08_string_algorithms
diff options
context:
space:
mode:
Diffstat (limited to '08_string_algorithms')
-rwxr-xr-x08_string_algorithms/a.outbin0 -> 27192 bytes
-rw-r--r--08_string_algorithms/finding_borders_1732.cpp23
-rw-r--r--08_string_algorithms/finding_periods_1733.cpp29
-rw-r--r--08_string_algorithms/string_matching_1753.cpp32
-rw-r--r--08_string_algorithms/word_combinations_1731.cpp63
5 files changed, 147 insertions, 0 deletions
diff --git a/08_string_algorithms/a.out b/08_string_algorithms/a.out
new file mode 100755
index 0000000..4556b32
--- /dev/null
+++ b/08_string_algorithms/a.out
Binary files differ
diff --git a/08_string_algorithms/finding_borders_1732.cpp b/08_string_algorithms/finding_borders_1732.cpp
new file mode 100644
index 0000000..d5cbbd4
--- /dev/null
+++ b/08_string_algorithms/finding_borders_1732.cpp
@@ -0,0 +1,23 @@
1#include <algorithm>
2#include <iostream>
3#include <string>
4#include <vector>
5
6int main() {
7 std::string s;
8 std::cin >> s;
9 std::vector<size_t> z(s.size(), 0), c;
10 z[0] = s.size();
11 for (size_t i = 1, j = 0, k = 0; i < s.size(); i++) {
12 if (j < i || z[i-k] == j-i) {
13 for (j = std::max(i, j); j < s.size() && s[j] == s[j-i]; j++) ;
14 z[k=i] = j-i;
15 } else z[i] = std::min(z[i-k], j-i);
16 if (z[i] == s.size()-i)
17 c.push_back(z[i]);
18 }
19 std::sort(c.begin(), c.end());
20 for (auto x : c)
21 std::cout << x << " ";
22 std::cout << "\n";
23}
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}
diff --git a/08_string_algorithms/string_matching_1753.cpp b/08_string_algorithms/string_matching_1753.cpp
new file mode 100644
index 0000000..148ed03
--- /dev/null
+++ b/08_string_algorithms/string_matching_1753.cpp
@@ -0,0 +1,32 @@
1#include <iostream>
2#include <string>
3#include <vector>
4
5struct S {
6 std::string w;
7 std::string t;
8
9 size_t size() const { return w.size() + 1 + t.size(); }
10
11 char operator[](size_t i) const {
12 if (i < w.size()) return w.at(i);
13 if (i > w.size()) return t.at(i-w.size()-1);
14 return '$';
15 }
16};
17
18int main() {
19 S s;
20 std::cin >> s.t >> s.w;
21 size_t c{0};
22 std::vector<size_t> z(s.size(), 0);
23 z[0] = s.w.size();
24 for (size_t i = 1, j = 0, k = 0; i < s.size(); i++) {
25 if (j < i || z[i-k] == j-i) {
26 for (j = std::max(i, j); j < s.size() && s[j] == s[j-i]; j++) ;
27 z[k=i] = j-i;
28 } else z[i] = std::min(z[i-k], j-i);
29 c += z[i] == s.w.size();
30 }
31 std::cout << c << "\n";
32}
diff --git a/08_string_algorithms/word_combinations_1731.cpp b/08_string_algorithms/word_combinations_1731.cpp
new file mode 100644
index 0000000..8a26b91
--- /dev/null
+++ b/08_string_algorithms/word_combinations_1731.cpp
@@ -0,0 +1,63 @@
1#include <iostream>
2#include <queue>
3#include <string>
4#include <string_view>
5#include <vector>
6
7struct TrieNode {
8 char c;
9 size_t d;
10 bool isend;
11 std::vector<TrieNode> next;
12
13 TrieNode(char x, size_t y) : c{x}, d{y}, isend{false}, next() {}
14};
15
16void push(std::string_view s, TrieNode& t) {
17 if (s.empty()) {
18 t.isend = true;
19 return;
20 }
21 for (auto& u : t.next) {
22 if (s[0] == u.c) {
23 push(s.substr(1), u);
24 return;
25 }
26 }
27 t.next.push_back(TrieNode(s[0], t.d+1));
28 push(s.substr(1), t.next.back());
29}
30
31const TrieNode* next(const TrieNode* w, char c) {
32 for (size_t i = 0; i < w->next.size(); i++)
33 if (w->next[i].c == c)
34 return &w->next[i];
35 return nullptr;
36}
37
38int f(std::vector<int>& t, std::string_view s, const TrieNode& d, size_t i) {
39 static constexpr long long mod = 1e9+7;
40
41 if (t[i] != -1) return t[i];
42
43 t[i] = 0;
44 for (const TrieNode* w = &d; w != nullptr; w = next(w, s[w->d]))
45 if (w->isend)
46 t[i] = (t[i] + f(t, s.substr(w->d), d, i+w->d)) % mod;
47 return t[i];
48}
49
50int main() {
51 std::string s, u;
52 size_t k;
53 std::cin >> s >> k;
54 TrieNode d('\0', 0);
55 for (size_t i = 0; i < k; i++) {
56 std::cin >> u;
57 push(u, d);
58 }
59
60 std::vector<int> t(s.size()+1, -1);
61 t[s.size()] = 1;
62 std::cout << f(t, s, d, 0) << "\n";
63}

Generated with cgit - Back to sebastiano.tronto.net