// this is a simple C code for Algorithm 3 of the paper // "Reconstructing a suffix array" // note that for the suffix array and the input string x we use C indexing // starting from 0 rather than the more common indexing used in the paper // starting from 1 // Therefore we cannot use 0 in the usual meaning, and hence introduce a // special symbol null // We also introduce a sspecial symbol empty for use with the stack // Note that the stack is created with size N which may not be needed, // see the paper #include "stdafx.h" #include #include #include int N; // length of the input string #define null -1 #define empty -2 void algorithm3(int* LSA,int* LCP,char* x); void sort(int* Next,int* from,int* to,int type,char* x); void modLCP(int* Next,int* LCP, int from,int to,int type,int last); int* compute_LSA(char* x); #define _debug // uncomment the above line and recompile for debugging //The following macors were just used for debugging #define ShowInd() {\ printf(" ");\ for(int _i=0;_i=0;_i--)\ if (_i==top)\ printf("Stack:\n%d\n",Stack[_i]);\ else\ printf("%d\n",Stack[_i]) #define lcp(cur) ((Tail[cur]==null)?(LCP[cur]):(LCP[Tail[Tail[cur]]])) #define nextTop() ((Top()==null)?(start):(Next[Top()])) // input LSA,LCP of the input string x // output modified LSA,LCP according to the permutation as defined in sort() // function algorithm3 ----------------------------------------------------- void algorithm3(int* LSA,int* LCP1,char* x) { int* Next=new int[N]; // create Next[] int* LCP=new int[N]; // create LCP[] int* Stack=new int[N]; // create Stack - the size of stack need not bee N (see the paper), // however for simplicity here we are suing N int top=null; // initialize Stack to empty int* Tail; // will become Tail[] int start; // "pointer" to beginning of the list Next[] int cur; // "pointer" to current "node" of the list Next[] int prev; // "pointer" to previous "node" of the list Next[] int from; // "pointer" to beginning of the family to be sorted int LE; // "pointer" to node immediately preceeding 'from' int RE; // "pointer" to node immediately following the end // of the family int type; // the size of the common prefix of all suffixes in // the family int modify_start; // a flag to indicate if 'start' needs to be modified // after sorting the family int small_family; // a flag indicating if the family has two (small) // or more members int last; // the LCP of the very last member of the family // before sort int famend; // "pointer" to the last member of the family // after sort int i, j, type1; // auxiliary variables // transform LSA to Next[], and modify LCP1 into LCP start=LSA[0]; LCP[start]=LCP1[1]; for(i=0;itype1) { // case B1 type=LCP[famend]; // decrease type prev=cur; cur=Next[cur]; continue; }else if (LCP[famend]==type1) { // case B2 Pop(); type=type1; prev=cur; cur=Next[cur]; if (cur==null) break; // break the while loop to perform the final // sort of the 0-family // 'prev' points to the very last item in Next continue; }else{ // hence LCP[famend]