Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #ifdef TEST
- #define debug(...) printf(__VA_ARGS__)
- #else
- #define debug(...) (void)0
- #endif
- #define EB emplace_back
- #define MP make_pair
- #define ST first
- #define ND second
- using namespace std;
- using ll = long long;
- using llu = long long unsigned;
- using pii = pair<int,int>;
- /************************/
- ll HashValue = 01234567;
- char cmd[100], opt_buf[1000];
- stringstream ss;
- void AddAnswer(const char *s) {
- debug("%s\n", s);
- int n = strlen(s);
- for (int i = 0; i < n; ++i)
- HashValue = (HashValue * 131ll + s[i]) % 1000000123;
- }
- void PrintAnswer() {
- printf("%lld\n", HashValue);
- }
- class Cmp{
- public:
- bool operator()(const void *a, const void *b);
- };
- struct Node{
- char name[20];
- Node *parent;
- set<Node*, Cmp> childs;
- int depth;
- Node(const char *s, int d):parent(NULL), depth(d){
- strcpy(name, s);
- }
- ~Node(){
- for(auto it = childs.begin() ; it != childs.end() ; ++it){
- delete *it;
- }
- }
- bool operator==(const Node &n){
- if(n.depth == depth && strcmp(n.name, name) == 0){
- return true;
- }
- return false;
- }
- Node* exist(const char *s){
- for(auto it = childs.begin() ; it != childs.end() ; ++it){
- char *nm = (*it)->name;
- if(strcmp(nm, s) == 0){
- return *it;
- }
- }
- return NULL;
- }
- void add(const char *s){
- Node *n = exist(s);
- if(n != NULL){
- memset(opt_buf, 0x00, sizeof(opt_buf));
- sprintf(opt_buf, "'%s' exist!\n", s);
- AddAnswer(opt_buf);
- }
- else{// passed check and actually add node
- Node *ch = new Node(s, depth+1);
- ch->parent = this;
- childs.emplace(ch);
- }
- }
- void rm(const char *s){
- Node *n = exist(s);
- if(n == NULL){
- memset(opt_buf, 0x00, sizeof(opt_buf));
- sprintf(opt_buf, "'%s' not exist!\n", s);
- AddAnswer(opt_buf);
- }
- else{
- childs.erase(n);
- delete n;
- }
- }
- };
- bool Cmp::operator()(const void *a, const void *b){
- Node *na = (Node*)a, *nb = (Node*)b;
- return strcmp(na->name, nb->name) < 0;
- }
- int main(){
- int n;
- Node *root = new Node("Ground", 0), *now;
- now = root;
- while(n--){
- ss.clear();
- fgets(cmd, 100, stdin);
- ss << cmd;
- string tokens[10];
- int i = 0;
- while(ss){
- ss >> tokens[i++];
- }
- if(tokens[0] == "dig"){ // add node
- string &n = tokens[1];
- now->add(n.c_str());
- }
- if(tokens[0] == "collapse"){ // remove node
- string &n = tokens[1];
- now->rm(n.c_str());
- }
- if(tokens[0] == "go"){
- if(tokens[1] == "to"){ // go to node
- string &n = tokens[2];
- Node *nd = now->exist(n.c_str());
- if(nd == NULL){
- memset(opt_buf, 0x00, sizeof(opt_buf));
- sprintf(opt_buf, "'%s' not exist!\n", n.c_str());
- AddAnswer(opt_buf);
- }
- else{
- now = nd;
- }
- }
- if(tokens[1] == "back"){ // go to parent
- if(now->parent == NULL){
- memset(opt_buf, 0x00, sizeof(opt_buf));
- sprintf(opt_buf, "You are on the ground!");
- AddAnswer(opt_buf);
- }
- else{
- now = now->parent;
- }
- }
- }
- if(tokens[0] == "where"){
- if(tokens[1] == "am"){ // report parents
- vector<string> opt;
- Node *forward = now;
- int cnt = 1;
- opt.EB(string(forward->name));
- // prepare opt
- while(forward->parent != NULL && cnt < 100){
- opt.EB(string(forward->parent->name));
- ++cnt;
- forward = forward->parent;
- }
- bool flag = false;
- // seq opt
- for(auto it = opt.rbegin() ; it != opt.rend() ; ++it){
- if(forward->parent == NULL || flag){
- AddAnswer("-> ");
- flag = true;
- }
- AddAnswer(it->c_str());
- }
- AddAnswer("\n");
- }
- if(tokens[1] == "can"){ // report child
- int cnt = 0;
- auto it = now->childs.begin();
- bool flag = false;
- for(; it != now->childs.end() && cnt<100 ; ++it, cnt++){
- if(flag){
- AddAnswer(" ");
- }
- AddAnswer((*it)->name);
- }
- if(it != now->childs.end()){
- AddAnswer(" ...");
- }
- AddAnswer("\n");
- }
- }
- }
- PrintAnswer();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement