diff --git a/Code/suffix_array_test.cpp b/Code/suffix_array_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d0f97c8862e7c5e8875eb39fe408dbdf7d9d9d4c --- /dev/null +++ b/Code/suffix_array_test.cpp @@ -0,0 +1,81 @@ +#include <bits/stdc++.h> +using namespace std; + +#define Mod(x,y) (((x)%(y)+(y))%(y)) +#define rep(i, a, b) for(ll (i) = (a); (i) < (b); ++(i)) +#define all(x) begin(x), end(x) +#define pb push_back +#define gcd __gcd +#define sz(x) (ll)(x.size()) + +typedef long long ll; +typedef unsigned long long ull; +typedef pair<ll, ll> pii; +typedef vector<ll> vi; +typedef vector<pii> vii; + +const bool debug = false; + +struct SuffixArray { + vi sa, lcp; + SuffixArray(string& s, int lim=256) { // or basic_string<int> + int n = sz(s) + 1, k = 0, a, b; + vi x(all(s)+1), y(n), ws(max(n, lim)), rank(n); + sa = lcp = y, iota(all(sa), 0); + for (int j = 0, p = 0; p < n; j = max(1, j * 2), lim = p) { + p = j, iota(all(y), n - j); + rep(i,0,n) if (sa[i] >= j) y[p++] = sa[i] - j; + fill(all(ws), 0); + rep(i,0,n) ws[x[i]]++; + rep(i,1,lim) ws[i] += ws[i - 1]; + for (int i = n; i--;) sa[--ws[x[y[i]]]] = y[i]; + swap(x, y), p = 1, x[sa[0]] = 0; + rep(i,1,n) a = sa[i - 1], b = sa[i], x[b] = + (y[a] == y[b] && y[a + j] == y[b + j]) ? p - 1 : p++; + } + rep(i,1,n) rank[sa[i]] = i; + for (int i = 0, j; i < n - 1; lcp[rank[i++]] = k) + for (k && k--, j = sa[rank[i] - 1]; + s[i + k] == s[j + k]; k++); + } +}; + +string ob_la_di = "DesmondhasabarrowinthemarketplaceMollyisthesingerinabandDesmondsaystoMolly,\"Girl,Ilikeyourface\"AndMollysaysthisasshetakeshimbythehand"; + +string longest_repeating_substring(string s) { + struct SuffixArray suffix_array(s); + int i_max, mx = -1; + int ind = 0; + for (int i : suffix_array.sa) { + if (suffix_array.lcp[ind] > mx) + i_max = i, mx = suffix_array.lcp[ind]; + ind++; + } + return s.substr(i_max, mx); +} + +void solve() { + struct SuffixArray suffix_array(ob_la_di); + cout << string(ob_la_di.size()+20, '-') << '\n'; + cout << "Printing the suffix array with associated lcp!\n"; + cout << "Pattern\n"; + cout << "lcp\t| suffix\n"; + + int ind = 0; + for (int i : suffix_array.sa) { + cout << suffix_array.lcp[ind] << "\t| " << ob_la_di.substr(i) << endl; + ind++; + } + cout << string(ob_la_di.size()+20, '-') << '\n'; + + cout << "longest repeating substring is:\n"; + cout << longest_repeating_substring(ob_la_di) << endl; +} + + +int main() { + ios::sync_with_stdio(0);cin.tie(0); + cout << setprecision(15) << fixed; + + solve(); +} \ No newline at end of file diff --git a/Code/trie.cpp b/Code/trie.cpp new file mode 100644 index 0000000000000000000000000000000000000000..de2addbd28290fdf8250fbc5f91cd3554b796d58 --- /dev/null +++ b/Code/trie.cpp @@ -0,0 +1,37 @@ +const int SIGMA = 26; + +typedef struct TrieNode{ + struct TrieNode *children[SIGMA]; + bool isEndOfWord; +} TrieNode; + +TrieNode* getNode() { + TrieNode *node = new TrieNode; + node->isEndOfWord = false; + rep(i, 0, SIGMA) node->children[i] = NULL; + return node; +} + +void insert(TrieNode* root, string key) { + TrieNode *node = root; + + rep(i, 0, key.size()) { + int ind = key[i]-'a'; + if (!node->children[ind]) node->children[ind] = getNode(); + node = node->children[ind]; + } + + node->isEndOfWord = true; +} + +bool search(TrieNode* root, string key) { + TrieNode* node = root; + + rep(i, 0, key.size()) { + int ind = key[i]-'a'; + if (!node->children[ind]) return false; + node = node->children[ind]; + } + + return node->isEndOfWord; +} \ No newline at end of file diff --git a/Slides/Pragmatic_Programming_Week7.pdf b/Slides/Pragmatic_Programming_Week7.pdf new file mode 100644 index 0000000000000000000000000000000000000000..5b9f56cf138e2205ed2b93e2b9518d2a76678e63 Binary files /dev/null and b/Slides/Pragmatic_Programming_Week7.pdf differ