Skip to content

Commit 0a1fc34

Browse files
committed
Added Suffix Automata
1 parent be98b1f commit 0a1fc34

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

String Algorithms & Data Structures/Suffix Automata.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class SuffixAutomata {
88
int firstPos;
99
bool isClone;
1010
vector<int> backLink;
11+
1112
bool has ( char ch ) {
1213
return next[ charToInt(ch) ] != -1;
1314
}
@@ -16,10 +17,10 @@ class SuffixAutomata {
1617
int sz, last;
1718
vector< state > Pool;
1819

19-
void getAllOccurrence (int v, int P_length , vector<int> &ret ) {
20-
if (! Pool[v].isClone) ret.pb( Pool[v].firstPos - P_length + 1 );
21-
for (size_t i=0; i<Pool[v].backLink.size(); ++i)
22-
getAllOccurrence (Pool[v].backLink[i], P_length, ret);
20+
void getAllOccurrence ( int v , int P_length , vector<int> &ret ) {
21+
if ( !Pool[v].isClone ) ret.pb( Pool[v].firstPos - P_length + 1 );
22+
for ( size_t i = 0; i < Pool[v].backLink.size(); i++ )
23+
getAllOccurrence ( Pool[v].backLink[i] , P_length , ret);
2324
}
2425

2526
public :
@@ -29,7 +30,7 @@ class SuffixAutomata {
2930
}
3031

3132
void initialize(){
32-
rep(i,sz) Pool[i].backLink.clr, Pool[i].isClone = false, Pool[i].link = -1, Pool[i].len = 0;
33+
rep( i , sz ) Pool[i].backLink.clr, Pool[i].isClone = false, Pool[i].link = -1, Pool[i].len = 0;
3334
sz = last = 0;
3435
Pool[0].len = 0;
3536
Pool[0].link = -1;
@@ -44,53 +45,52 @@ class SuffixAutomata {
4445
Pool[cur].firstPos = Pool[cur].len - 1;
4546
Pool[cur].isClone = false;
4647
int p;
47-
for (p=last; p!=-1 && !Pool[p].has(c); p=Pool[p].link) Pool[p].next[ charToInt(c) ] = cur;
48-
if (p == -1) Pool[cur].link = 0;
48+
for ( p = last; p != -1 && !Pool[p].has(c) ; p = Pool[p].link ) Pool[p].next[ charToInt(c) ] = cur;
49+
if ( p == -1 ) Pool[cur].link = 0;
4950
else {
5051
int q = Pool[p].next[ charToInt(c) ];
51-
if (Pool[p].len + 1 == Pool[q].len)
52-
Pool[cur].link = q;
52+
if ( Pool[p].len + 1 == Pool[q].len ) Pool[cur].link = q;
5353
else {
5454
int clone = sz++;
55-
mem(Pool[clone].next,-1);
55+
mem( Pool[clone].next , -1 );
5656
Pool[clone].len = Pool[p].len + 1;
5757
Pool[clone].firstPos = Pool[p].firstPos;
5858
Pool[clone].isClone = true;
59-
memcpy(Pool[clone].next, Pool[q].next, sizeof (Pool[q].next));
59+
memcpy( Pool[clone].next , Pool[q].next , sizeof (Pool[q].next) );
6060
Pool[clone].link = Pool[q].link;
61-
for (; p!=-1 && Pool[p].next[charToInt(c)]==q; p=Pool[p].link)
62-
Pool[p].next[charToInt(c)] = clone;
61+
for ( ; p != -1 && Pool[p].next[charToInt(c)] == q; p = Pool[p].link )
62+
Pool[p].next[ charToInt(c) ] = clone;
6363
Pool[q].link = Pool[cur].link = clone;
6464
}
6565
}
6666
last = cur;
6767
}
6868

6969
void construct( string &str ){
70-
for( int i=0; i<sz(str); i++ ) addChar(str[i]);
70+
rep( i , sz(str) ) addChar(str[i]);
7171
for( int v=1; v<sz; v++ ) {
7272
if( Pool[v].link != -1 )
73-
Pool[ Pool[v].link ].backLink.pb(v);
73+
Pool[ Pool[v].link ].backLink.pb( v );
7474
}
7575
}
7676

7777
int findState ( string &pattern ) {
7878
int now = 0, length = 0;
79-
for( int i=0; i<sz(pattern); i++ ){
79+
for( int i=0; i < sz(pattern); i++ ) {
8080
char ch = pattern[i];
8181
while( now && !Pool[now].has( ch ) ) {
8282
now = Pool[now].link;
8383
length = Pool[now].len;
8484
}
85-
if(Pool[now].has(ch)){
85+
if( Pool[now].has(ch) ){
8686
now = Pool[now].next[ch-'a'];
8787
length++;
8888
}
8989
}
9090
return ( length == sz(pattern) ? now : -1 );
9191
}
9292

93-
vector<int> getAllOccurrence( string &pattern ){
93+
vector<int> getAllOccurrence( string &pattern ) {
9494
vector<int> ret;
9595
int v = findState( pattern );
9696
if( v == -1 ) return ret;

0 commit comments

Comments
 (0)