Skip to content

Instantly share code, notes, and snippets.

@karino2
Created July 26, 2026 02:52
Show Gist options
  • Select an option

  • Save karino2/e22da339a5eab350f450f1e6f6809ab0 to your computer and use it in GitHub Desktop.

Select an option

Save karino2/e22da339a5eab350f450f1e6f6809ab0 to your computer and use it in GitHub Desktop.
buildEsa.cpp
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include "esa.hxx"
using namespace std;
int readFile(const char* fn, vector<int>& T){
FILE* fp = fopen(fn, "rb");
if (fp == NULL){
cerr << "cannot open " << fn << endl;
return -1;
}
if (fseek(fp, 0, SEEK_END) != 0){
cerr << "cannot fseek " << fn << endl;
fclose(fp);
return -1;
}
int n = ftell(fp);
rewind(fp);
if (n < 0){
cerr << "cannot ftell " << fn << endl;
fclose(fp);
return -1;
}
T.resize(n);
if (fread(&T[0], sizeof(unsigned char), (size_t)n, fp) != (size_t) n){
cerr << "fread error " << fn << endl;
fclose(fp);
return -1;
}
fclose(fp);
return 0;
}
void printSubstr(const vector<int>& T, const int beg){
for (int i = beg; i < T.size(); ++i){
int c = T[i];
cout << (isspace((char)c) ? '_' : (char)c);
}
}
int main(int argc, char* argv[]){
vector<int> T;
istreambuf_iterator<char> isit(cin);
istreambuf_iterator<char> end;
size_t origLen = 0;
while (isit != end){
T.push_back((unsigned char)(*isit++));
}
origLen = T.size();
vector<int> SA(T.size());
vector<int> L (T.size());
vector<int> R (T.size());
vector<int> D (T.size());
int k = 0x100;
cerr << " n:" << T.size() << endl;
cerr << "alpha:" << k << endl;
int nodeNum = 0;
if (esaxx(T.begin(), SA.begin(),
L.begin(), R.begin(), D.begin(),
(int)T.size(), k, nodeNum) == -1){
return -1;
}
cout << " Suffix Array:" << endl;
cout << "i\tSA\tL\tR\tD\tsubstring:" << endl;
cout << "--------------------------------" << endl;
for (int i = 0; i < T.size(); i++) {
cout << i << "\t" << SA[i] << "\t" << L[i] << "\t" << R[i] << "\t" << D[i] << "\t";
printSubstr(T, SA[i]);
cout << endl;
}
cout << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment