Advertisement
Ginger_samurai

Untitled

Jun 8th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include<vector>
  2. #include <string>
  3. #include<algorithm>
  4. #include <iostream>
  5. #include <queue>
  6. #include<set>
  7. #include<stack>
  8. #include<cmath>
  9. #include<math.h>
  10. #include<map>
  11. using namespace std;
  12. #define ll long long
  13. #define pll pair<long long, long long>
  14. #define mp make_pair
  15. #define all(a) a.begin(), a.end()
  16. #define rall(a) a.rbegin(), a.rend()
  17. #define fs first
  18. #define sd second
  19. const ll inf = 1e9 + 123, llinf = 1e18 + 123;
  20. void xru() {
  21.  setlocale(LC_ALL, "rus");
  22.  /*freopen(".in", "r", stdin);
  23.  freopen(".out", "w", stdout);*/
  24.  ios_base::sync_with_stdio(false);
  25.  cin.tie(NULL);
  26.  cout.tie(NULL);
  27. }
  28. void run(){
  29.  cout<<endl;
  30.  system("pause");
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40. struct vertex{
  41.     vertex* to[26];
  42.     bool term;
  43.     vertex(){
  44.         for(ll i = 0; i<26; i++ ) to[i] = nullptr;
  45.         term = false;
  46.     }
  47. };
  48.  
  49. vertex* root = new vertex();
  50.  
  51. void push(string& s){
  52.     vertex* v = root;
  53.     for(char c: s){
  54.         c-='a';
  55.         if(v->to[c] == nullptr) v->to[c] = new vertex();
  56.         v = v->to[c];
  57.     }
  58.     v->term = true;
  59. }
  60.  
  61. bool check(string& s){
  62.     vertex* v = root;
  63.         for(char c: s){
  64.         c-='a';
  65.         if(v->to[c] == nullptr) return false;
  66.         v = v->to[c];
  67.     }
  68.     return v->term;
  69. }
  70.  
  71. string str = "";
  72. void write(vertex* v = root){
  73.     if(v->term) cout<<str<<endl;
  74.  
  75.     for(ll i = 0; i<26; i++){
  76.         if(v->to[i] != nullptr){
  77.             str.push_back(i + 'a');
  78.             write(v->to[i]);
  79.             str.pop_back();
  80.         }
  81.     }
  82. }
  83.  
  84.  
  85.  
  86.  
  87.  
  88. int main(){
  89.     while(true){
  90.         char c;
  91.         cin>>c;
  92.         if(c == '+'){
  93.             string s;
  94.             cin>>s;            
  95.             push(s);
  96.         } else if (c == '?'){
  97.             string s;
  98.             cin>>s;            
  99.             if(check(s)) cout<<"Yes\n";
  100.             else cout<<"No\n";
  101.         } else if (c == '#'){
  102.             write();
  103.         } else{
  104.             break;
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement