Advertisement
sabertooth09

Untitled

Sep 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include<ext/pb_ds/assoc_container.hpp>
  3. #include<ext/pb_ds/tree_policy.hpp>
  4. #include<ext/pb_ds/trie_policy.hpp>
  5. #include<ext/pb_ds/tag_and_trait.hpp>
  6. using namespace std;
  7. using namespace __gnu_pbds;
  8.  
  9. template <typename T>
  10. using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  11. template<typename T1,typename T2>
  12. using ordered_map = tree<T1, T2, less<T1>, rb_tree_tag, tree_order_statistics_node_update>;
  13. using trie_type=trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update>;
  14.  
  15. void print_prefix_match(const trie_type& t, const string& key)
  16. {
  17.     typedef trie_type::const_iterator       const_iterator;
  18.     typedef pair<const_iterator, const_iterator>    pair_type;
  19.  
  20.     cout << "All keys whose prefix matches \"" << key << "\":" << endl;
  21.  
  22.     const pair_type match_range = t.prefix_range(key);
  23.     for (const_iterator it = match_range.first; it != match_range.second; ++it)
  24.         cout << *it << ' ';
  25.     cout << endl;
  26. }
  27. int main()
  28. {
  29.     ordered_set<int>  s;
  30.     s.insert(1);
  31.     s.insert(3);
  32.     cout << s.order_of_key(2) << endl; // the number of elements in the s less than 2
  33.     cout << *s.find_by_order(0) << endl; // print the 0-th smallest number in s(0-based)
  34.     trie_type t;
  35.  
  36.     // Insert some entries.
  37.     t.insert("I");
  38.     assert(t.insert("wish").second == true);
  39.     assert(t.insert("that").second == true);
  40.     assert(t.insert("I").second == false);
  41.     assert(t.insert("could").second == true);
  42.     assert(t.insert("ever").second == true);
  43.     assert(t.insert("see").second == true);
  44.     assert(t.insert("a").second == true);
  45.     assert(t.insert("poem").second == true);
  46.     assert(t.insert("lovely").second == true);
  47.     assert(t.insert("as").second == true);
  48.     assert(t.insert("a").second == false);
  49.     assert(t.insert("trie").second == true);
  50.  
  51.     // Now search for prefixes.
  52.     print_prefix_match(t, "");
  53.     print_prefix_match(t, "a");
  54.     print_prefix_match(t, "as");
  55.     print_prefix_match(t, "ad");
  56.     print_prefix_match(t, "t");
  57.     print_prefix_match(t, "tr");
  58.     print_prefix_match(t, "trie");
  59.     print_prefix_match(t, "zzz");
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement