Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suffix array #162

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Codigos/String/Suffix-Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ Estrutura que conterá inteiros que representam os índices iniciais de todos os

Também constrói a tabela LCP (Longest Common Prefix).

- `sa[i]` = Índice inicial do i-ésimo menor sufixo.
- `ra[i]` = Rank do sufixo que começa em `i`.
- `LCP[i]` = Comprimento do maior prefixo comum entre os sufixos `sa[i]` e `sa[i-1]`.

* Complexidade de tempo (Pré-Processamento): $\mathcal{O}(|S| \cdot \log(|S|))$
* Complexidade de tempo (Contar ocorrências de \(S\) em \(T\)): $\mathcal{O}(|S| \cdot \log(|T|))$
15 changes: 6 additions & 9 deletions Codigos/String/Suffix-Array/suffix_array.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const int MAX_N = 5e5 + 5;

const int MAX = 5e5 + 5;
struct suffix_array {
string s;
int n, sum, r, ra[MAX_N], sa[MAX_N], auxra[MAX_N], auxsa[MAX_N], c[MAX_N], lcp[MAX_N];
int n, sum, r, ra[MAX], sa[MAX], auxra[MAX], auxsa[MAX], c[MAX], lcp[MAX];
void counting_sort(int k) {
memset(c, 0, sizeof(c));
for (int i = 0; i < n; i++) c[(i + k < n) ? ra[i + k] : 0]++;
Expand All @@ -15,12 +14,10 @@ struct suffix_array {
counting_sort(k);
counting_sort(0);
auxra[sa[0]] = r = 0;
for (int i = 1; i < n; i++) {
auxra[sa[i]] =
(ra[sa[i]] == ra[sa[i - 1]] && ra[sa[i] + k] == ra[sa[i - 1] + k])
? r
: ++r;
}
for (int i = 1; i < n; i++)
if (ra[sa[i]] == ra[sa[i - 1]] && ra[sa[i] + k] == ra[sa[i - 1] + k])
auxra[sa[i]] = r;
else auxra[sa[i]] = ++r;
for (int i = 0; i < n; i++) ra[i] = auxra[i];
if (ra[sa[n - 1]] == n - 1) break;
}
Expand Down
Loading