summaryrefslogtreecommitdiffstats
path: root/02-data_structures/03-hash_tables/03-hash_substring/hash_substring.cpp
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2016-11-13 22:22:32 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2016-11-13 22:22:32 +0100
commite6b725d69c07d4a582d0aadc915def99d872dfde (patch)
tree29dad552d2303a507ccfcef37305fb816f09b183 /02-data_structures/03-hash_tables/03-hash_substring/hash_substring.cpp
parentedb46efabf8a4dd64a9a04eb9840b2ea82100103 (diff)
downloadcoursera-e6b725d69c07d4a582d0aadc915def99d872dfde.zip
coursera-e6b725d69c07d4a582d0aadc915def99d872dfde.tar.gz
Algorithms : add 02-data_structures 03-hash_tables
Diffstat (limited to '02-data_structures/03-hash_tables/03-hash_substring/hash_substring.cpp')
-rw-r--r--02-data_structures/03-hash_tables/03-hash_substring/hash_substring.cpp38
1 files changed, 38 insertions, 0 deletions
diff --git a/02-data_structures/03-hash_tables/03-hash_substring/hash_substring.cpp b/02-data_structures/03-hash_tables/03-hash_substring/hash_substring.cpp
new file mode 100644
index 0000000..3a3680a
--- /dev/null
+++ b/02-data_structures/03-hash_tables/03-hash_substring/hash_substring.cpp
@@ -0,0 +1,38 @@
+#include <iostream>
+#include <string>
+#include <vector>
+
+using std::string;
+typedef unsigned long long ull;
+
+struct Data {
+ string pattern, text;
+};
+
+Data read_input() {
+ Data data;
+ std::cin >> data.pattern >> data.text;
+ return data;
+}
+
+void print_occurrences(const std::vector<int>& output) {
+ for (size_t i = 0; i < output.size(); ++i)
+ std::cout << output[i] << " ";
+ std::cout << "\n";
+}
+
+std::vector<int> get_occurrences(const Data& input) {
+ const string& s = input.pattern, t = input.text;
+ std::vector<int> ans;
+ for (size_t i = 0; i + s.size() <= t.size(); ++i)
+ if (t.substr(i, s.size()) == s)
+ ans.push_back(i);
+ return ans;
+}
+
+
+int main() {
+ std::ios_base::sync_with_stdio(false);
+ print_occurrences(get_occurrences(read_input()));
+ return 0;
+}