Advertisement
smatskevich

Seminar13

May 15th, 2023
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <memory>
  4. #include <unordered_map>
  5. #include <vector>
  6.  
  7. struct Node {
  8.   std::map<char, std::unique_ptr<Node>> Go;
  9.   bool Terminal = false;
  10. };
  11. class Trie {
  12.  public:
  13.   Trie() : root(new Node) {}
  14.   void Add(const std::string& s);
  15.   void Print() const { std::string prefix; Print(prefix, root); }
  16.  private:
  17.   std::unique_ptr<Node> root;
  18.   static void Print(std::string& prefix, const std::unique_ptr<Node>& n);
  19. };
  20. void Trie::Add(const std::string& s) {
  21.   Node* current = root.get();
  22.   for (char c : s) {
  23.     if (auto it = current->Go.find(c); it != current->Go.end())
  24.       current = it->second.get();
  25.     else
  26.       current = (current->Go[c] = std::make_unique<Node>()).get();
  27.   }
  28.   current->Terminal = true;
  29. }
  30.  
  31. void Trie::Print(std::string& prefix, const std::unique_ptr<Node>& n) {
  32.   std::cout << prefix << (n->Terminal ? "\t\t - Terminal" : "") << "\n";
  33.   // if (n->Terminal) result.emplace_back(prefix);
  34.   for (auto& [c, target] : n->Go) {
  35.     prefix.push_back(c);
  36.     Print(prefix, target);
  37.     prefix.pop_back();
  38.   }
  39. }
  40.  
  41. int main1() {
  42.   Trie t;
  43.   t.Add("arc");
  44.   t.Add("b");
  45.   t.Add("car");
  46.   t.Add("c");
  47.   t.Add("cat");
  48.   t.Print();
  49.  
  50.   std::cout << sizeof(std::unique_ptr<Node>) << std::endl;
  51.   return 0;
  52. }
  53.  
  54. class OrdString {
  55.   size_t capacity;
  56.   char* buf;
  57.   size_t size;
  58. };
  59.  
  60. class OptString {
  61.   size_t capacity;
  62.  
  63.   union {
  64.     struct {
  65.       char* buf;
  66.       size_t size;
  67.     } heapbuf;
  68.  
  69.     char stackbuf[sizeof(heapbuf)];
  70.   };
  71. };
  72.  
  73. int main2() {
  74.   std::cout << sizeof(OrdString) << std::endl;
  75.   std::cout << sizeof(OptString) << std::endl;
  76.   std::cout << sizeof(std::string) << std::endl;
  77.   int min_cap = (sizeof(OrdString) - 1)/sizeof(char) > 2 ?
  78.               (sizeof(OrdString) - 1)/sizeof(char) : 2;
  79.   std::cout << min_cap << std::endl;
  80.   return 0;
  81. }
  82.  
  83. struct ACNode {
  84.   char in_symbol;  // 1
  85.   bool is_terminal;  // 1 + 2
  86.   int parent;  // 4
  87.   int suffix_link = -1;  // 4 + 4
  88.   std::unordered_map<char, int> go;  // 40. Сохраненные переходы по символам из текста + Переходы по букве исходные
  89. };
  90.  
  91. int main() {
  92.   ACNode n;
  93.   std::cout << sizeof(n) << std::endl;
  94.   return 0;
  95. }
  96. class AhoCorasik {
  97.  public:
  98.   explicit AhoCorasik(std::vector<std::string>& patterns);
  99.  
  100.  private:
  101.   std::vector<ACNode> nodes;
  102.  
  103.   int GoTo(int nodeid, char symbol);
  104.   int Suffix(int nodeid);
  105. };
  106.  
  107. int AhoCorasik::GoTo(int nodeid, char symbol) {
  108.   ACNode& node = nodes[nodeid];
  109.   if (auto it = node.go.find(symbol); it != node.go.end()) return it->second;
  110.   return node.go[symbol] = GoTo(Suffix(nodeid), symbol);
  111. }
  112.  
  113. int AhoCorasik::Suffix(int nodeid) {
  114.   ACNode& node = nodes[nodeid];
  115.   if (node.suffix_link >= 0) return node.suffix_link;
  116.   return node.suffix_link = GoTo(Suffix(node.parent), node.in_symbol);
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement