Advertisement
Guest User

Untitled

a guest
May 31st, 2012
857
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <map>
  5. using namespace std;
  6.  
  7. struct node {
  8.     int parent;
  9.     map<char, int> to;
  10.     string str;
  11.  
  12.     node(int parent, string &str) {
  13.         this->parent = parent;
  14.         this->str = str;
  15.     }
  16. };
  17.  
  18. vector<node> trie;
  19.  
  20. void init() {
  21.     trie.push_back(node(-1, string("")));
  22. }
  23.  
  24. void split(int n, int pos) {
  25.     trie.push_back(node(trie[n].parent, trie[n].str.substr(0, pos)));
  26.     trie[trie[n].parent].to[trie[n].str[0]] = trie.size() - 1;
  27.     trie[trie.size() - 1].to[trie[n].str[pos]] = n;
  28.     trie[n].str = trie[n].str.substr(pos);
  29. }
  30.  
  31. void add(string &str) {
  32.     int curnode = 0;
  33.     int curpos = 0;
  34.     for (int i = 0; i < str.length(); i++) {
  35.         if (curpos < trie[curnode].str.length()) {
  36.             if (trie[curnode].str[curpos] == str[i]) {
  37.                 curpos++;
  38.                 continue;
  39.             } else {
  40.                 split(curnode, curpos);
  41.                 curnode = trie.size() - 1;
  42.             }
  43.         }
  44.         if (trie[curnode].to.count(str[i]) == 0) {
  45.             trie.push_back(node(curnode, str.substr(i)));
  46.             trie[curnode].to[str[i]] = trie.size() - 1;
  47.         }
  48.         curnode = trie[curnode].to[str[i]];
  49.         curpos = 1;
  50.     }
  51.     if (curpos < trie[curnode].str.length()) {
  52.         split(curnode, curpos);
  53.         curnode = trie.size() - 1;
  54.     }
  55.     if (trie[curnode].to.count('$') == 0) {
  56.         trie[curnode].to['$'] = -1;
  57.     }
  58. }
  59.  
  60. bool contains(string &str) {
  61.     int curnode = 0;
  62.     int curpos = 0;
  63.     for (int i = 0; i < str.length(); i++) {
  64.         if (curpos < trie[curnode].str.length()) {
  65.             if (trie[curnode].str[curpos] == str[i]) {
  66.                 curpos++;
  67.                 continue;
  68.             } else {
  69.                 return false;
  70.             }
  71.         }
  72.         if (trie[curnode].to.count(str[i]) == 0) {
  73.             return false;
  74.         }
  75.         curnode = trie[curnode].to[str[i]];
  76.         curpos = 1;
  77.     }
  78.     if (curpos < trie[curnode].str.length()) {
  79.         return false;
  80.     }
  81.     if (trie[curnode].to.count('$') == 0) {
  82.         return false;
  83.     }
  84.     return true;
  85. }
  86.  
  87. int main() {
  88.     init();
  89.     while (true) {
  90.         string command;
  91.         cin >> command;
  92.         if (command == "exit") {
  93.             break;
  94.         } else if (command == "+") {
  95.             string str;
  96.             cin >> str;
  97.             add(str);
  98.         } else if (command == "?") {
  99.             string str;
  100.             cin >> str;
  101.             cout << (contains(str) ? "YES" : "NO") << endl;
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement