Advertisement
smatskevich

Seminar12

Apr 28th, 2022
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <unordered_set>
  4. #include <vector>
  5.  
  6. std::vector<int> OddPalindroms(const std::string& s) {
  7.   std::vector<int> data(s.size(), 1);
  8.   int big_index = 0;
  9.   for (int i = 1; i < s.size(); ++i) {
  10.     if (i < big_index + data[big_index] - 1)
  11.       data[i] = std::min(data[2 * big_index - i], big_index + data[big_index] - i);
  12.     for (int j = data[i]; i - j >= 0 && i + j < s.size() && s[i - j] == s[i + j]; ++j) ++data[i];
  13.     if (i + data[i] > big_index + data[big_index]) big_index = i;
  14.   }
  15.   return data;
  16. }
  17.  
  18. int main0() {
  19.   std::string s;
  20.   std::cin >> s;
  21.   std::vector<int> odd_palindroms = OddPalindroms(s);
  22.   for (int v : odd_palindroms) std::cout << v << " ";
  23.   return 0;
  24. }
  25.  
  26. struct Node {
  27. //  char Letter = 0;  // Буква, по которой пришли в узел.
  28.   bool IsTerminal = false;
  29.   std::vector<Node*> Children;
  30.  
  31.   ~Node() { for(auto x : Children) delete x; }
  32. };
  33.  
  34. // Children [0, 1, 2, 3, 4, 5, 6, 7, ..., 32, ..., 48, 49, ... ]
  35. //                                        ' '          '1'
  36. //                                        nullptr     Node*
  37.  
  38. class Trie {
  39.  public:
  40.   ~Trie() { delete start; }
  41.   void Add(const std::string& s);
  42.   void Print() const;
  43.  
  44.  private:
  45.   Node* start = new Node;
  46.   void DFSPrint(std::string& s, Node* current) const;
  47. };
  48.  
  49. // hello
  50. // hell
  51. void Trie::Add(const std::string& s) {
  52.   Node* cur_node = start;
  53.   for (int i = 0; i < s.size(); ++i) {
  54.     if (cur_node->Children.empty()) cur_node->Children.assign(256, nullptr);
  55.     if (!cur_node->Children[s[i]]) {
  56.       cur_node->Children[s[i]] = new Node;
  57.     }
  58.     cur_node = cur_node->Children[s[i]];
  59.     cur_node->IsTerminal |= (i == s.size() - 1);
  60.   }
  61. }
  62.  
  63. void Trie::Print() const {
  64.   std::string s;
  65.   DFSPrint(s, start);
  66. }
  67.  
  68. void Trie::DFSPrint(std::string& s, Node* current) const {
  69.   if (current->IsTerminal) std::cout << s << "\n";
  70.   for (int i = 0; i < current->Children.size(); ++i) {
  71.     if (current->Children[i]) {
  72.       s.push_back(char(i));
  73.       DFSPrint(s, current->Children[i]);
  74.       s.pop_back();
  75.     }
  76.   }
  77. }
  78.  
  79. int main1() {
  80.   Trie trie;
  81.   int n = 0; std::cin >> n;
  82.   for (int i = 0; i < n; ++i) {
  83.     std::string s;
  84.     std::cin >> s;
  85.     trie.Add(s);
  86.   }
  87.   trie.Print();
  88.   return 0;
  89. }
  90.  
  91. int main() {
  92.   std::unordered_set<int> x;
  93.   x.insert(3);
  94.   std::cout << x.bucket_count();
  95.   return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement