Advertisement
Emiliatan

e495

Oct 23rd, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.55 KB | None | 0 0
  1. /* e495            */
  2. /* AC (3ms, 368KB) */
  3. #pragma warning( disable : 4996 )
  4. #include <cstdio>
  5. #include <cstring>
  6. #include <cstdint>
  7. #include <cmath>
  8. #include <algorithm>
  9. #include <tuple>
  10. #define ios_jazz ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
  11.  
  12. using namespace std;
  13.  
  14. using int16 = short;
  15. using uint16 = unsigned short;
  16. using uint = unsigned int;
  17. using int64 = long long;
  18. using uint64 = unsigned long long;
  19. using pii = pair<int, int>;
  20.  
  21. /* main code */
  22.  
  23. #include <iostream>
  24. #include <vector>
  25. #include <string>
  26.  
  27. enum {FOLD, DATA};
  28.  
  29. struct node
  30. {
  31.     string name = "";
  32.     bool format = false;
  33.  
  34.     node* fa = nullptr;
  35.     vector<node*> childs;
  36.  
  37.     node() { fa = nullptr; format = false; };
  38.     node(string s, bool f) : name(s), format(f) { fa = nullptr; };
  39.  
  40.     bool isFind(node target)
  41.     {
  42.         for (auto it : childs)
  43.             if (it->name == target.name)
  44.                 return true;
  45.  
  46.         return false;
  47.     }
  48.  
  49.     void create(string fn, bool ff)
  50.     {
  51.         if (format == DATA) return;
  52.  
  53.         if (isFind(node{ fn, ff })) return;
  54.  
  55.         childs.emplace_back(new node(fn, ff));
  56.         childs[childs.size() - 1]->fa = this;
  57.  
  58.         return;
  59.     }
  60.  
  61.     void show()
  62.     {
  63.         cout << name << ":" << '\n';
  64.  
  65.         if (childs.size() == 0)
  66.         {
  67.             cout << "-(None)" << '\n' << '\n';
  68.  
  69.             return;
  70.         }
  71.  
  72.         using psb = pair<string, bool>;
  73.  
  74.         vector<psb> v;
  75.  
  76.         for (auto it : childs)
  77.             v.emplace_back(psb{ it->name, it->format });
  78.  
  79.         sort(v.begin(), v.end());
  80.  
  81.         for (auto it : v)
  82.             if (it.second == FOLD)
  83.             {
  84.                 cout << "-" << it.first << " " << "FOLD" << '\n';
  85.             }
  86.  
  87.         for (auto it : v)
  88.             if (it.second == DATA)
  89.             {
  90.                 cout << "-" << it.first << " " << "DATA" << '\n';
  91.             }
  92.  
  93.         cout << '\n';
  94.        
  95.     }
  96. };
  97. node* nowNode;
  98.  
  99. enum {Father, Child};
  100. node *copyer, *cuter[2];
  101. string lastcutcopy;
  102.  
  103. void copy(string fn)
  104. {
  105.     for (auto it : nowNode->childs)
  106.         if (it->name == fn)
  107.         {
  108.             copyer = it;
  109.             cuter[Father] = cuter[Child] = nullptr;
  110.             lastcutcopy = "copy";
  111.             return;
  112.         }
  113. }
  114. void cut(string fn)
  115. {
  116.     for (auto it : nowNode->childs)
  117.         if (it->name == fn)
  118.         {
  119.             cuter[Father] = nowNode, cuter[Child] = it;
  120.             copyer = nullptr;
  121.             lastcutcopy = "cut";
  122.             return;
  123.         }
  124. }
  125.  
  126. void __dfs(node* now)
  127. {
  128.     nowNode->childs.emplace_back(new node(now->name, now->format));
  129.     nowNode->childs.back()->fa = nowNode;
  130.     nowNode = nowNode->childs.back();
  131.  
  132.     for (auto it : now->childs)
  133.     {
  134.         if (it->format == DATA)
  135.         {
  136.             nowNode->childs.emplace_back(new node(it->name, it->format));
  137.             continue;
  138.         }
  139.         else
  140.             __dfs(it);
  141.     }
  142.     nowNode = nowNode->fa;
  143. }
  144.  
  145. void pasta()
  146. {
  147.     if (lastcutcopy == "copy" && copyer != nullptr)
  148.     {
  149.         bool _exist = false;
  150.         for (auto it : nowNode->childs)
  151.         {
  152.             _exist |= (it->name == copyer->name);
  153.         }
  154.  
  155.         if (!_exist)
  156.         {
  157.             __dfs(copyer);
  158.  
  159.             lastcutcopy = "";
  160.             copyer = nullptr;
  161.         }
  162.     }
  163.     else if (lastcutcopy == "cut" && cuter[Father] != nullptr && cuter[Child] != nullptr)
  164.     {
  165.         bool _exist = false;
  166.         for (auto it : nowNode->childs)
  167.         {
  168.             _exist |= (it->name == cuter[Child]->name);
  169.         }
  170.  
  171.         if (!_exist)
  172.         {
  173.             nowNode->childs.emplace_back(cuter[Child]);
  174.             nowNode->childs.back()->fa = nowNode;
  175.  
  176.             for (auto it = cuter[Father]->childs.begin(); it != cuter[Father]->childs.end(); ++it)
  177.             {
  178.                 if ((*it)->name == cuter[Child]->name && (*it)->format == cuter[Child]->format)
  179.                 {
  180.                     cuter[Father]->childs.erase(it);
  181.                     break;
  182.                 }
  183.             }
  184.             lastcutcopy = "";
  185.             cuter[Father] = cuter[Child] = nullptr;
  186.         }
  187.     }
  188. }
  189.  
  190. void Move(string fn)
  191. {
  192.     bool _exist = false;
  193.     int i;
  194.     for (i = 0; i < nowNode->childs.size(); ++i)
  195.     {
  196.         if (nowNode->childs[i]->name == fn && nowNode->childs[i]->format == FOLD)
  197.         {
  198.             _exist = true;
  199.             break;
  200.         }
  201.     }
  202.     if (_exist)
  203.         nowNode = nowNode->childs[i];
  204. }
  205. void back()
  206. {
  207.     if (nowNode->fa == nullptr)
  208.         return;
  209.     nowNode = nowNode->fa;
  210. }
  211.  
  212. int main()
  213. {
  214.     auto FormatConvert = [](string FF) {return (FF == "FOLD" ? FOLD : DATA); };
  215.  
  216.     nowNode = new node("C", FOLD);
  217.     nowNode->fa = nullptr;
  218.  
  219.     string command, Fname, Fformat;
  220.     while (cin >> command && command != "end")
  221.     {
  222.         if (command == "cut" || command == "copy")
  223.         {
  224.             cin >> Fname;
  225.            
  226.             if (command == "cut")
  227.                 cut(Fname);
  228.             else
  229.                 copy(Fname);
  230.         }
  231.         else if (command == "show")
  232.         {
  233.             nowNode->show();
  234.         }
  235.         else if (command == "create")
  236.         {
  237.             cin >> Fname >> Fformat;
  238.             nowNode->create(Fname, FormatConvert(Fformat));
  239.         }
  240.         else if (command == "pasta")
  241.         {
  242.             pasta();
  243.         }
  244.         else if (command == "move")
  245.         {
  246.             cin >> Fname;
  247.  
  248.             Move(Fname);
  249.         }
  250.         else if (command == "return")
  251.         {
  252.             back();
  253.         }
  254.     }
  255.  
  256.     return 0;
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement