Skip to content
Snippets Groups Projects
Commit 96a1af5d authored by Max Nilsson's avatar Max Nilsson
Browse files

String stuff

parent 9914f6f2
No related branches found
No related tags found
No related merge requests found
#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
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
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment