Search code examples
c++algorithmdata-structuresarray-algorithms

Suffix Range c++


I am trying to build a Suffix range that is

if I have strings "catalog" "catalyst" "ban" "bany"

then suffix tree will be like

                            .
                           / \
                          c   b
                         /     \
                        a       a
                       /         \
                      t           n
                     / \         / \        
                    a   a       $   y 
                   /     \         / \
                  l       l       $    $
                 /         \
                o           y         
               /             \
              g               s
             / \               \
            $   $               t
                                /\
                               $   $

I want to find Suffix range of each string now .. that if I take string "Cat" then it should give me a range enclosing all its suffixes to which "cat" is a prefix. I need to use sentinels to separate each string.. may be a "$"

Can any one suggest me a best way to find out this using c++ . Any references will be helpfull. thank you


Solution

  • Here is, I guess, the most concise answer. :)

    set<string> s;
    string word = "ABC"
    //Inserts.
    // e.g. s.insert("ABCD");
    
    for(set<string>::iterator it=s.begin();it!=s.end();++it)
        if(!(*it).compare(0,word.size(),word))
            cout<<*it<<endl;
    

    Tested code! :P