Advertisement
jw910731

npsc

Nov 15th, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.94 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #ifdef TEST
  3.     #define debug(...) printf(__VA_ARGS__)
  4. #else
  5.     #define debug(...) (void)0
  6. #endif
  7. #define EB emplace_back
  8. #define MP make_pair
  9. #define ST first
  10. #define ND second
  11. using namespace std;
  12. using ll = long long;
  13. using llu = long long unsigned;
  14. using pii = pair<int,int>;
  15. /************************/
  16. ll HashValue = 01234567;
  17. char cmd[100], opt_buf[1000];
  18. stringstream ss;
  19. void AddAnswer(const char *s) {
  20.     debug("%s\n", s);
  21.     int n = strlen(s);
  22.     for (int i = 0; i < n; ++i)
  23.     HashValue = (HashValue * 131ll + s[i]) % 1000000123;
  24. }
  25. void PrintAnswer() {
  26.     printf("%lld\n", HashValue);
  27. }
  28. class Cmp{
  29. public:
  30.     bool operator()(const void *a, const void *b);
  31. };
  32. struct Node{
  33.     char name[20];
  34.     Node *parent;
  35.     set<Node*, Cmp> childs;
  36.     int depth;
  37.     Node(const char *s, int d):parent(NULL), depth(d){
  38.         strcpy(name, s);
  39.     }
  40.     ~Node(){
  41.         for(auto it = childs.begin() ; it != childs.end() ; ++it){
  42.             delete *it;
  43.         }
  44.     }
  45.     bool operator==(const Node &n){
  46.         if(n.depth == depth && strcmp(n.name, name) == 0){
  47.             return true;
  48.         }
  49.         return false;
  50.     }
  51.     Node* exist(const char *s){
  52.         for(auto it = childs.begin() ; it != childs.end() ; ++it){
  53.             char *nm = (*it)->name;
  54.             if(strcmp(nm, s) == 0){
  55.                 return *it;
  56.             }
  57.         }
  58.         return NULL;
  59.     }
  60.     void add(const char *s){
  61.         Node *n = exist(s);
  62.         if(n != NULL){
  63.             memset(opt_buf, 0x00, sizeof(opt_buf));
  64.             sprintf(opt_buf, "'%s' exist!\n", s);
  65.             AddAnswer(opt_buf);
  66.         }
  67.         else{// passed check and actually add node
  68.             Node *ch = new Node(s, depth+1);
  69.             ch->parent = this;
  70.             childs.emplace(ch);
  71.         }
  72.        
  73.     }
  74.     void rm(const char *s){
  75.         Node *n = exist(s);
  76.         if(n == NULL){
  77.             memset(opt_buf, 0x00, sizeof(opt_buf));
  78.             sprintf(opt_buf, "'%s' not exist!\n", s);
  79.             AddAnswer(opt_buf);
  80.         }
  81.         else{
  82.             childs.erase(n);
  83.             delete n;
  84.         }
  85.     }
  86. };
  87.  
  88. bool Cmp::operator()(const void *a, const void *b){
  89.     Node *na = (Node*)a, *nb = (Node*)b;
  90.     return strcmp(na->name, nb->name) < 0;
  91. }
  92.  
  93. int main(){
  94.     int n;
  95.     Node *root = new Node("Ground", 0), *now;
  96.     now = root;
  97.     while(n--){
  98.         ss.clear();
  99.         fgets(cmd, 100, stdin);
  100.         ss << cmd;
  101.         string tokens[10];
  102.         int i = 0;
  103.         while(ss){
  104.             ss >> tokens[i++];
  105.         }
  106.         if(tokens[0] == "dig"){ // add node
  107.             string &n = tokens[1];
  108.             now->add(n.c_str());
  109.         }
  110.         if(tokens[0] == "collapse"){ // remove node
  111.             string &n = tokens[1];
  112.             now->rm(n.c_str());
  113.         }
  114.         if(tokens[0] == "go"){
  115.             if(tokens[1] == "to"){ // go to node
  116.                 string &n = tokens[2];
  117.                 Node *nd = now->exist(n.c_str());
  118.                 if(nd == NULL){
  119.                     memset(opt_buf, 0x00, sizeof(opt_buf));
  120.                     sprintf(opt_buf, "'%s' not exist!\n", n.c_str());
  121.                     AddAnswer(opt_buf);
  122.                 }
  123.                 else{
  124.                     now = nd;
  125.                 }
  126.             }
  127.             if(tokens[1] == "back"){ // go to parent
  128.                 if(now->parent == NULL){
  129.                     memset(opt_buf, 0x00, sizeof(opt_buf));
  130.                     sprintf(opt_buf, "You are on the ground!");
  131.                     AddAnswer(opt_buf);
  132.                 }
  133.                 else{
  134.                     now = now->parent;
  135.                 }
  136.             }
  137.         }
  138.         if(tokens[0] == "where"){
  139.             if(tokens[1] == "am"){ // report parents
  140.                 vector<string> opt;
  141.                 Node *forward = now;
  142.                 int cnt = 1;
  143.                 opt.EB(string(forward->name));
  144.                 // prepare opt
  145.                 while(forward->parent != NULL && cnt < 100){
  146.                     opt.EB(string(forward->parent->name));
  147.                     ++cnt;
  148.                     forward = forward->parent;
  149.                 }
  150.                 bool flag = false;
  151.                 // seq opt
  152.                 for(auto it = opt.rbegin() ; it != opt.rend() ; ++it){
  153.                     if(forward->parent == NULL || flag){
  154.                         AddAnswer("-> ");
  155.                         flag = true;
  156.                     }
  157.                     AddAnswer(it->c_str());
  158.                 }
  159.                 AddAnswer("\n");
  160.             }
  161.             if(tokens[1] == "can"){ // report child
  162.                 int cnt = 0;
  163.                 auto it = now->childs.begin();
  164.                 bool flag = false;
  165.                 for(; it != now->childs.end() && cnt<100 ; ++it, cnt++){
  166.                     if(flag){
  167.                         AddAnswer(" ");
  168.                     }
  169.                     AddAnswer((*it)->name);
  170.                 }
  171.                 if(it != now->childs.end()){
  172.                     AddAnswer(" ...");
  173.                 }
  174.                 AddAnswer("\n");
  175.             }
  176.         }
  177.     }
  178.     PrintAnswer();
  179.     return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement