From 4d81cf1577b2a0da863e798c1df87f7b3d4d0d31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= <joaomarcosnet10@gmail.com> Date: Fri, 12 Jul 2024 18:16:30 -0300 Subject: [PATCH 1/2] fix clang format --- Codigos/String/Suffix-Array/README.md | 4 ++++ Codigos/String/Suffix-Array/suffix_array.cpp | 20 ++++++++++---------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Codigos/String/Suffix-Array/README.md b/Codigos/String/Suffix-Array/README.md index 6182f050..fee28221 100644 --- a/Codigos/String/Suffix-Array/README.md +++ b/Codigos/String/Suffix-Array/README.md @@ -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|))$ diff --git a/Codigos/String/Suffix-Array/suffix_array.cpp b/Codigos/String/Suffix-Array/suffix_array.cpp index 155e225d..e965e383 100644 --- a/Codigos/String/Suffix-Array/suffix_array.cpp +++ b/Codigos/String/Suffix-Array/suffix_array.cpp @@ -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]++; @@ -16,10 +15,11 @@ struct suffix_array { 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; + 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; @@ -39,9 +39,9 @@ struct suffix_array { for (int i = 0; i < n; i++) ra[i] = s[i], sa[i] = i; build_sa(); build_lcp(); - // for (int i = 0; i < n; i++) - // printf("%2d: %s\n", sa[i], s.c_str() + - // sa[i]); + //for (int i = 0; i < n; i++) + //printf("%2d: %s\n", sa[i], s.c_str() + + //sa[i]); } int operator[](int i) { return sa[i]; } } sa; From f5db317511fa4b14ca20a8df940ee566d60f311e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= <joaomarcosnet10@gmail.com> Date: Wed, 28 Aug 2024 16:23:53 -0300 Subject: [PATCH 2/2] clang-format --- Codigos/String/Suffix-Array/suffix_array.cpp | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/Codigos/String/Suffix-Array/suffix_array.cpp b/Codigos/String/Suffix-Array/suffix_array.cpp index e965e383..df1cb205 100644 --- a/Codigos/String/Suffix-Array/suffix_array.cpp +++ b/Codigos/String/Suffix-Array/suffix_array.cpp @@ -14,13 +14,10 @@ struct suffix_array { counting_sort(k); counting_sort(0); auxra[sa[0]] = r = 0; - for (int i = 1; i < n; i++) { - if (ra[sa[i]] == ra[sa[i - 1]] && ra[sa[i] + k] == ra[sa[i - 1] + k]) { + 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; - } - } + else auxra[sa[i]] = ++r; for (int i = 0; i < n; i++) ra[i] = auxra[i]; if (ra[sa[n - 1]] == n - 1) break; } @@ -39,9 +36,9 @@ struct suffix_array { for (int i = 0; i < n; i++) ra[i] = s[i], sa[i] = i; build_sa(); build_lcp(); - //for (int i = 0; i < n; i++) - //printf("%2d: %s\n", sa[i], s.c_str() + - //sa[i]); + // for (int i = 0; i < n; i++) + // printf("%2d: %s\n", sa[i], s.c_str() + + // sa[i]); } int operator[](int i) { return sa[i]; } } sa;