Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <string>
  4. #include <sstream>
  5. #include <iosfwd>
  6. #include <algorithm>
  7. #include <assert.h>
  8. #include <array>
  9.  
  10. using TDict = std::unordered_map<std::string, std::string>;
  11. TDict dict = { {"ehllo","hello"}, {"dlorw","world"} };
  12.  
  13. //////////////////////RECONSTRUCT-EASY///////////
  14.  
  15. std::string reconstruct(std::string str)
  16. {
  17.     std::string tok, res;
  18.     std::stringstream st(str);
  19.  
  20.     while (std::getline(st, tok, ' '))
  21.     {
  22.         if (res.size() != 0)
  23.         {
  24.             res += " ";
  25.         }
  26.  
  27.         std::sort(tok.begin(),tok.end());
  28.         auto iter = dict.find(tok);
  29.         assert(iter != dict.end());
  30.         res += iter->second;
  31.     }
  32.  
  33.     return res;
  34. }
  35.  
  36. //////////////////////RECONSTRUCT-HARD///////////
  37.  
  38. const int azSz = ('z' - 'a') + 1;
  39.  
  40. struct PrefixNode
  41. {
  42.     std::array<PrefixNode*, azSz> children;
  43.     std::string word;
  44.  
  45.     PrefixNode* GetChild(char c)
  46.     {
  47.         return children[c - 'a'];
  48.     }
  49.  
  50.     PrefixNode* GetOrCreateChild(char c)
  51.     {
  52.         if (!GetChild(c))
  53.         {
  54.             children[c - 'a'] = new PrefixNode();
  55.         }
  56.  
  57.         return GetChild(c);
  58.     }
  59.  
  60.     PrefixNode()
  61.     {
  62.         children.fill(nullptr);
  63.     }
  64. };
  65.  
  66. class PrefixTree
  67. {
  68. public:
  69.     PrefixTree(TDict& dict);
  70.     void Insert(const std::string& mangled, const std::string& real);
  71.  
  72.     bool Reconstruct(const std::string& str, std::string& ret);
  73.  
  74. private:
  75.     PrefixNode root;
  76. };
  77.  
  78. PrefixTree::PrefixTree(TDict& d)
  79. {
  80.     for (auto iter = d.begin(); iter != d.end(); ++iter)
  81.     {
  82.         Insert(iter->first, iter->second);
  83.     }
  84. }
  85.  
  86. void PrefixTree::Insert(const std::string& mangled, const std::string& real)
  87. {
  88.     auto* curr = &root;
  89.     for (auto& c : mangled)
  90.     {
  91.         auto* next = curr->GetOrCreateChild(c);
  92.         if (!next)
  93.         {
  94.             next = new PrefixNode();
  95.         }
  96.  
  97.         curr = next;
  98.     }
  99.  
  100.     assert(curr->word.size() == 0);
  101.     curr->word = real;
  102. }
  103.  
  104. bool PrefixTree::Reconstruct(const std::string& str, std::string& ret)
  105. {
  106.     auto* curr = &root;
  107.     for (auto& c : str)
  108.     {
  109.         auto* next = curr->GetChild(c);
  110.         if (!next)
  111.         {
  112.             return false;
  113.         }
  114.  
  115.         curr = next;
  116.     }
  117.  
  118.     if (curr->word.size() == 0)
  119.     {
  120.         return false;
  121.     }
  122.  
  123.     ret = curr->word;
  124.     return true;
  125. }
  126.  
  127. std::string reconstructHard(std::string str)
  128. {
  129.     PrefixTree tree(dict);
  130.  
  131.     std::string buf,fin;
  132.  
  133.     for (auto& c : str)
  134.     {
  135.         auto iter = std::upper_bound(buf.begin(), buf.end(), c);
  136.         buf.insert(iter, c);
  137.         std::string ret;
  138.  
  139.         if (tree.Reconstruct(buf, ret))
  140.         {
  141.             buf = "";
  142.  
  143.             if (fin.size() != 0)
  144.             {
  145.                 fin += " ";
  146.             }
  147.             fin += ret;
  148.         }
  149.     }
  150.  
  151.     return fin;
  152. }
  153.  
  154. //////////////////////////////////////////////////
  155.  
  156. int main()
  157. {
  158.     std::cout << reconstruct("helol drowl helol drowl").c_str();
  159.     std::cout << reconstructHard("heloldrowlheloldrowl").c_str();
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement