C++ Program to Implement Suffix Array

This C++ Program demonstrates the implementation of Suffix Array.

Here is source code of the C++ Program to demonstrate the implementation of Suffix Array. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Implement Suffix Array
  3.  */
  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <string>
  8. using namespace std;
  9.  
  10. class SuffixArray
  11. {
  12.     private:
  13.         string *text;
  14.         int length;
  15.         int *index;
  16.         string *suffix;
  17.     public:
  18.         SuffixArray(string text)
  19.         {
  20.             this->text = new string[text.length()]; 
  21.             for (int i = 0; i < text.length(); i++)
  22.             {
  23.                 this->text[i] = text.substr(i, 1);
  24.             } 
  25.             this->length = text.length();
  26.             this->index = new int[length];
  27.             for (int i = 0; i < length; i++)
  28.             {
  29.                 index[i] = i;
  30.             }	
  31.             suffix = new string[length];
  32.         }
  33.  
  34.         void createSuffixArray()
  35.         {   
  36.             for(int index = 0; index < length; index++)	
  37.             {
  38.                 string text = "";
  39.                 for (int text_index = index; text_index < length; text_index++)
  40.                 {
  41.                     text += this->text[text_index];
  42.                 } 
  43.                 suffix[index] = text;
  44.             }
  45.             int back;
  46.             for (int iteration = 1; iteration < length; iteration++)
  47.             {
  48.                 string key = suffix[iteration];
  49.                 int keyindex = index[iteration];
  50.                 for (back = iteration - 1; back >= 0; back--)
  51.                 {
  52.                     if (suffix[back].compare(key) > 0)
  53.                     {
  54.                         suffix[back + 1] = suffix[back];
  55.                         index[back + 1] = index[back];
  56.                     }
  57.                     else
  58.                     {
  59.                         break;
  60.                     }
  61.                 }
  62.                 suffix[back + 1] = key;
  63.                 index[back + 1 ] = keyindex;
  64.             }
  65.             cout<<"SUFFIX \t INDEX"<<endl;
  66.             for (int iterate = 0; iterate < length; iterate++)
  67.             {		
  68.                 cout<<suffix[iterate] << "\t" << index[iterate]<<endl;
  69.             }
  70.         }
  71. };
  72.  
  73. int main()
  74. {
  75.     string text;
  76.     cout<<"Enter the Text String: ";
  77.     cin>>text;
  78.     SuffixArray suffixarray(text);
  79.     suffixarray.createSuffixArray();
  80. }

$ g++ suffix_array.cpp
$ a.out
 
Enter the Text String: sanfoundry
SUFFIX        INDEX
anfoundry       1
dry             7
foundry         3
ndry            6
nfoundry        2
oundry          4
ry              8
sanfoundry      0
undry           5
y               9
 
 
------------------
(program exited with code: 1)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.