baotrung217

SuffixTrie

Nov 27th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. const int ALPHABET_SIZE = 26;
  5.  
  6. // Trie node
  7. struct TrieNode
  8. {
  9.     TrieNode * children[ALPHABET_SIZE];
  10.     bool isEndOfWord;
  11. };
  12.  
  13. // Returns new trie node (initialized to NULLs)
  14. TrieNode *getNode(void)
  15. {
  16.     struct TrieNode *pNode =  new TrieNode;
  17.  
  18.     pNode->isEndOfWord = false;
  19.     for (int i = 0; i < ALPHABET_SIZE; i++)
  20.     {
  21.         pNode->children[i] = NULL;
  22.     }
  23.     return pNode;
  24. }
  25.  
  26. // If not present, inserts key into trie
  27. // If the key is prefix of trie node, just
  28. // marks leaf node
  29. void insert(TrieNode *root, std::string key)
  30. {
  31.     TrieNode *pCrawl = root;
  32.  
  33.     for (int i = 0; i < key.length(); i++)
  34.     {
  35.         int index = key[i] - 'a';
  36.         if (!pCrawl->children[index])
  37.             pCrawl->children[index] = getNode();
  38.  
  39.         pCrawl = pCrawl->children[index];
  40.     }
  41.     // mark last node as leaf
  42.     pCrawl->isEndOfWord = true;
  43. }
  44.  
  45. std::string findSubstringInWord(std::string word, TrieNode * root)
  46. {
  47.     int lenLongSubstr = 0;
  48.     int longestPos = 0;
  49.     int length = 0;
  50.  
  51.     for (int i=0; i<word.length(); ++i)
  52.     {
  53.         TrieNode *pCrawl = root;
  54.  
  55.         for (int j=i; j<word.length(); ++j)
  56.         {
  57.             int index = word[j] - 'a';
  58.             if (!pCrawl->children[index])
  59.                 break;
  60.             pCrawl = pCrawl->children[index];
  61.             length = j - i + 1;
  62.             if (pCrawl->isEndOfWord && length>lenLongSubstr)
  63.             {
  64.                 lenLongSubstr = length;
  65.                 longestPos = i;
  66.             }
  67.         }
  68.     }
  69.  
  70.     if (lenLongSubstr==0)
  71.     {
  72.         return word;
  73.     }
  74.     else
  75.     {
  76.         word.insert(longestPos, "[");
  77.         word.insert(longestPos + lenLongSubstr + 1, "]");
  78.         return word;
  79.     }
  80. }
  81.  
  82. std::vector<std::string> findSubstrings(std::vector<std::string> words, std::vector<std::string> parts)
  83. {
  84. }
  85.  
  86. // driver program to test above functions
  87. int main()
  88. {
  89.     //vector<string> words = {"Apple", "Melon", "Orange", "Watermelon"};
  90.     //vector<string> parts = {"a", "mel", "lon", "el", "An", "ter"};
  91.     //vector<string> words = {"Watermelon"};
  92.     //vector<string> parts = {"a", "mel", "el"};
  93.  
  94.     //std::vector<std::string> result = findSubstrings(words, parts);
  95.     //for (std::vector<std::string>::iterator it = result.begin(); it!=result.end(); ++it)
  96.     //{
  97.     //    cout << "  " << *it;
  98.     //}
  99.  
  100.     std::string word = "watermelon";
  101.     std::string part = "ter";
  102.  
  103.     TrieNode *root = getNode();
  104.     insert(root, part);
  105.  
  106.     std::cout << findSubstringInWord(word, root);
  107.  
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment