Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 92d9326

Browse files
Add SuffixArray
1 parent 2532d2c commit 92d9326

File tree

7 files changed

+91
-31
lines changed

7 files changed

+91
-31
lines changed

‎Library/Strings/SuffixArray.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
vector<int> suffixArray(string s){
5+
int n = (s += "$").size(); //if s is vector, push_back(-INF);
6+
vector<int> sf(n), ord(n), aux(n), cnt(n);
7+
iota(begin(sf), end(sf), 0);
8+
sort(begin(sf), end(sf), [&](int i, int j){ return s[i] < s[j]; });
9+
10+
int cur = ord[sf[0]] = 0;
11+
for(int i=1; i<n; i++)
12+
ord[sf[i]] = s[sf[i]] == s[sf[i-1]] ? cur : ++cur;
13+
14+
for(int k=1; cur+1 < n && k < n; k<<=1){
15+
cnt.assign(n, 0);
16+
for(auto &i : sf) i = (i-k+n)%n, cnt[ord[i]]++;
17+
for(int i=1; i<n; i++) cnt[i] += cnt[i-1];
18+
for(int i=n-1; i>=0; i--) aux[--cnt[ord[sf[i]]]] = sf[i];
19+
sf.swap(aux);
20+
21+
aux[sf[0]] = cur = 0;
22+
for(int i=1; i<n; i++)
23+
aux[sf[i]] = ord[sf[i]] == ord[sf[i-1]] &&
24+
ord[(sf[i]+k)%n] == ord[(sf[i-1]+k)%n] ? cur : ++cur;
25+
ord.swap(aux);
26+
}
27+
return vector<int>(begin(sf)+1, end(sf));
28+
}
29+
30+
vector<int> LCP(string &s, vector<int> &sf){
31+
int n = s.size();
32+
vector<int> lcp(n), pof(n);
33+
for(int i=0; i<n; i++) pof[sf[i]] = i;
34+
35+
for(int i=0, j, k=0; i<n; k?--k:k, i++){
36+
if(!pof[i]) continue;
37+
j = sf[pof[i]-1];
38+
while(i+k<n && j+k<n && s[i+k]==s[j+k]) k++;
39+
lcp[pof[i]] = k;
40+
}
41+
return lcp;
42+
}
43+
44+
/*LATEX_DESC_BEGIN***************************
45+
sf = suffixArray(s) -> **O(N log N)**
46+
LCP(s, sf) -> **O(N)**
47+
48+
**SuffixArray** -> index of suffix in lexicographic order
49+
LCP[i] -> **LargestCommonPrefix** of sufix at sf[i] and sf[i-1]
50+
LCP(i,j) = min(lcp[i+1...j])
51+
52+
To better understand, print: lcp[i] sf[i] s.substr(sf[i])
53+
*****************************LATEX_DESC_END*/

‎Library/Strings/ahoCorasick.cpp

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
#include <bits/stdc++.h>
22
using namespace std;
3+
const int ALPHA = 26, off = 'a';
4+
struct Node {
5+
Node* p = NULL;
6+
Node* sl = NULL;
7+
Node* ol = NULL;
8+
array<Node*, ALPHA> nxt;
9+
10+
char c;
11+
int idw = -1;
312

4-
template<const int ALPHA = 26, const int off = 'a'>
13+
Node(){ nxt.fill(NULL); }
14+
Node(Node* p, char c) : p(p), c(c) { nxt.fill(NULL); }
15+
};
16+
typedef Node* trie;
517
struct Aho {
6-
struct Node {
7-
Node* p = NULL;
8-
Node* sl = NULL;
9-
Node* ol = NULL;
10-
array<Node*, ALPHA> nxt;
11-
12-
char c;
13-
int idw = -1;
14-
15-
Node(){ nxt.fill(NULL); }
16-
Node(Node* p, char c) : p(p), c(c) { nxt.fill(NULL); }
17-
};
18-
typedef Node* trie;
19-
2018
trie root;
2119
int nwords = 0;
2220
Aho(){ root = new Node(); }
@@ -54,9 +52,9 @@ struct Aho {
5452
vector<bool> ans(nwords, 0);
5553
trie w = root;
5654
for(auto c : s){ c -= off;
57-
while(w && !w->nxt[c]) w = w->sl;
55+
while(w && !w->nxt[c]) w = w->sl;// trie next(w, c)
5856
w = w ? w->nxt[c] : root;
59-
57+
6058
for(trie z=w, nl; z; nl=z->ol, z->ol=NULL, z=nl)
6159
if(z->idw != -1) //get ALL occ: dont delete ol (may slow)
6260
ans[z->idw] = true;
@@ -65,12 +63,11 @@ struct Aho {
6563
}
6664
};
6765

66+
6867
/*LATEX_DESC_BEGIN***************************
6968
Aho-Corasick: Trie automaton to search multiple patterns in a text
69+
**Complexity:** O(SUM|P| + |S|) * ALPHA
7070
71-
Complexity: O(SUM|P| + |S|) * ALPHA
72-
73-
Aho<26,'a'> aho;
7471
for(auto p: patterns) aho.add(p);
7572
aho.buildSufixLink();
7673
auto ans = aho.findPattern(s);
@@ -80,4 +77,12 @@ outputLink -> edge to other pattern end (when p is a sufix of it)
8077
ALPHA -> Size of the alphabet. If big, consider changing nxt to map
8178
8279
To find ALL occurrences of all patterns, don't delete ol in findPattern. But it can be slow (at number of occ), so consider using DP on the automaton.
80+
If you need a **nextState** function, create it using the while in findPattern.
81+
if you need to **store node indexes** add int i to Node, and in Aho add this and change the new Node() to it:
82+
vector<trie> nodes;
83+
trie new_Node(trie p, char c){
84+
nodes.push_back(new Node(p, c));
85+
nodes.back()->i = nodes.size()-1;
86+
return nodes.back();
87+
}
8388
*****************************LATEX_DESC_END*/

‎SH12-Notebook.pdf

11.1 KB
Binary file not shown.

‎pdf/README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Para gerar o pdf execute o script shell.
44

55
```shell
6-
sh .\generate_pdf.sh
6+
sh ./generate_pdf.sh
77
```
88

99
Necessário que haja suporte para C++ e [latexmk](https://www.ctan.org/pkg/latexmk/)!
@@ -22,7 +22,7 @@ Para remover algum dos códigos do PDF, remova ou comente a linha dele com ```#`
2222

2323
Para gerar novamente a lista com todos os códigos e extras, rode:
2424
```shell
25-
python3 .\getContents.py > .\contents.txt
25+
python3 ./getContents.py > ./contents.txt
2626
```
2727

2828
## 📚 Extras
@@ -41,14 +41,14 @@ Você também pode adicionar seu arquivo tex diretamente na pasta de códigos. N
4141

4242
- [getContents.py](getContents.py): Altere ```code_dir = "../Library"``` para o path relativo dos seus códigos. **Lembre de rodar o getContents antes para ter a lista com os seus códigos!** <sup><sub>Confira também o arquivo para ver se todos os códigos que você quer estão lá e para remover coisas que você não quer que entrem.</sub></sup>
4343

44-
- [notebook.tex](notebook.tex): Se quiser mudar a quantidade de colunas, altere ```\begin{multicols*}{3}``` na linha ```119```. Mude também essas opções de títulos do seu PDF.
44+
- [notebook.tex](notebook.tex): Se quiser mudar a quantidade de colunas, altere ```\begin{multicols*}{3}``` na linha ```119```. Mude também essas opções de títulos do seu PDF e coloque o símbolo da sua universidade.
4545
```tex
4646
\fancyhead[L]{Universidade Federal de Pernambuco - SamuellH12} %line 103
4747
\fancyhead[L]{Universidade Federal de Pernambuco - SamuellH12} %line 108
4848
\title{\vspace{-4ex}\Large{SamuellH12 - ICPC Library}} %line 113
4949
```
5050

51-
- Opcional [generate_pdf.sh](generate_pdf.sh): O pdf gerado está sendo renomeado e movido para a pasta parent da atual. ```mv notebook.pdf ../SH12-Notebook.pdf``` (obs: só não mantenha como notebook.pdf)
51+
- Opcional [generate_pdf.sh](generate_pdf.sh): O pdf gerado está sendo renomeado e movido para a pasta parent da atual. ```mv notebook.pdf ../Notebook.pdf``` (obs: só não mantenha como notebook.pdf)
5252

5353
### :wrench: Outras personalizações
5454

@@ -104,16 +104,16 @@ hash < codigo.cpp
104104
```
105105

106106
```cpp
107-
string getHash(string s, int dig=3){
107+
string getHash(string s){
108108
ofstream ip("temp.cpp"); ip << s; ip.close();
109-
system("g++ -E -P -dD -fpreprocessed .\\temp.cpp | tr -d '[:space:]' | md5sum > hsh.temp");
110-
ifstream f("hsh.temp"); f >> s; f.close();
111-
return s.substr(0, dig);
109+
system("g++ -E -P -dD -fpreprocessed ./temp.cpp | tr -d '[:space:]' | md5sum > hsh.temp");
110+
ifstream fo("hsh.temp"); fo >> s; fo.close();
111+
return s.substr(0, 3);
112112
}
113113

114114
int main(){
115115
string l, t;
116-
vector<string> st(100);
116+
vector<string> st(10);
117117
while(getline(cin, l)){
118118
t = l;
119119
for(auto c : l)

‎pdf/contents.txt

102 Bytes
Binary file not shown.

‎pdf/generate_latex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ bool convert_files(const string& input_path, const string& output_path, string&
215215
else
216216
if(bgn == string::npos && end != string::npos){
217217
isInBlckDesc = false;
218-
block_desc += (block_desc.empty() ? "" : "\\\\") + line.substr(0, end);
218+
block_desc += (block_desc.empty() ? "" : "\\\\\n") + line.substr(0, end);
219219
line.erase(0, end + BLOCK_DESC_END.size());
220220
}
221221
else if(isInBlckDesc){

‎pdf/hash/md5hsh.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <bits/stdc++.h>
22
#define ll long long
33
using namespace std;
4+
#ifdef MAINFILE //LATEX_IGNORED_LINE
45

56
string getHash(string s){
67
ofstream ip("temp.cpp"); ip << s; ip.close();
@@ -9,7 +10,7 @@ string getHash(string s){
910
return s.substr(0, 3);
1011
}
1112

12-
int main_(){
13+
int main(){
1314
string l, t;
1415
vector<string> st(10);
1516
while(getline(cin, l)){
@@ -23,6 +24,7 @@ int main_(){
2324
return 0; //LATEX_IGNORED_LINE
2425
}
2526

27+
#endif //LATEX_IGNORED_LINE
2628
/*LATEX_DESC_BEGIN
2729
Call
2830
BLOCK_DESC_BEGIN g++ hash.cpp -o hash \\ hash < code.cpp BLOCK_DESC_END to get the hash of the code.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /