Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* e495 */
- /* AC (3ms, 368KB) */
- #pragma warning( disable : 4996 )
- #include <cstdio>
- #include <cstring>
- #include <cstdint>
- #include <cmath>
- #include <algorithm>
- #include <tuple>
- #define ios_jazz ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
- using namespace std;
- using int16 = short;
- using uint16 = unsigned short;
- using uint = unsigned int;
- using int64 = long long;
- using uint64 = unsigned long long;
- using pii = pair<int, int>;
- /* main code */
- #include <iostream>
- #include <vector>
- #include <string>
- enum {FOLD, DATA};
- struct node
- {
- string name = "";
- bool format = false;
- node* fa = nullptr;
- vector<node*> childs;
- node() { fa = nullptr; format = false; };
- node(string s, bool f) : name(s), format(f) { fa = nullptr; };
- bool isFind(node target)
- {
- for (auto it : childs)
- if (it->name == target.name)
- return true;
- return false;
- }
- void create(string fn, bool ff)
- {
- if (format == DATA) return;
- if (isFind(node{ fn, ff })) return;
- childs.emplace_back(new node(fn, ff));
- childs[childs.size() - 1]->fa = this;
- return;
- }
- void show()
- {
- cout << name << ":" << '\n';
- if (childs.size() == 0)
- {
- cout << "-(None)" << '\n' << '\n';
- return;
- }
- using psb = pair<string, bool>;
- vector<psb> v;
- for (auto it : childs)
- v.emplace_back(psb{ it->name, it->format });
- sort(v.begin(), v.end());
- for (auto it : v)
- if (it.second == FOLD)
- {
- cout << "-" << it.first << " " << "FOLD" << '\n';
- }
- for (auto it : v)
- if (it.second == DATA)
- {
- cout << "-" << it.first << " " << "DATA" << '\n';
- }
- cout << '\n';
- }
- };
- node* nowNode;
- enum {Father, Child};
- node *copyer, *cuter[2];
- string lastcutcopy;
- void copy(string fn)
- {
- for (auto it : nowNode->childs)
- if (it->name == fn)
- {
- copyer = it;
- cuter[Father] = cuter[Child] = nullptr;
- lastcutcopy = "copy";
- return;
- }
- }
- void cut(string fn)
- {
- for (auto it : nowNode->childs)
- if (it->name == fn)
- {
- cuter[Father] = nowNode, cuter[Child] = it;
- copyer = nullptr;
- lastcutcopy = "cut";
- return;
- }
- }
- void __dfs(node* now)
- {
- nowNode->childs.emplace_back(new node(now->name, now->format));
- nowNode->childs.back()->fa = nowNode;
- nowNode = nowNode->childs.back();
- for (auto it : now->childs)
- {
- if (it->format == DATA)
- {
- nowNode->childs.emplace_back(new node(it->name, it->format));
- continue;
- }
- else
- __dfs(it);
- }
- nowNode = nowNode->fa;
- }
- void pasta()
- {
- if (lastcutcopy == "copy" && copyer != nullptr)
- {
- bool _exist = false;
- for (auto it : nowNode->childs)
- {
- _exist |= (it->name == copyer->name);
- }
- if (!_exist)
- {
- __dfs(copyer);
- lastcutcopy = "";
- copyer = nullptr;
- }
- }
- else if (lastcutcopy == "cut" && cuter[Father] != nullptr && cuter[Child] != nullptr)
- {
- bool _exist = false;
- for (auto it : nowNode->childs)
- {
- _exist |= (it->name == cuter[Child]->name);
- }
- if (!_exist)
- {
- nowNode->childs.emplace_back(cuter[Child]);
- nowNode->childs.back()->fa = nowNode;
- for (auto it = cuter[Father]->childs.begin(); it != cuter[Father]->childs.end(); ++it)
- {
- if ((*it)->name == cuter[Child]->name && (*it)->format == cuter[Child]->format)
- {
- cuter[Father]->childs.erase(it);
- break;
- }
- }
- lastcutcopy = "";
- cuter[Father] = cuter[Child] = nullptr;
- }
- }
- }
- void Move(string fn)
- {
- bool _exist = false;
- int i;
- for (i = 0; i < nowNode->childs.size(); ++i)
- {
- if (nowNode->childs[i]->name == fn && nowNode->childs[i]->format == FOLD)
- {
- _exist = true;
- break;
- }
- }
- if (_exist)
- nowNode = nowNode->childs[i];
- }
- void back()
- {
- if (nowNode->fa == nullptr)
- return;
- nowNode = nowNode->fa;
- }
- int main()
- {
- auto FormatConvert = [](string FF) {return (FF == "FOLD" ? FOLD : DATA); };
- nowNode = new node("C", FOLD);
- nowNode->fa = nullptr;
- string command, Fname, Fformat;
- while (cin >> command && command != "end")
- {
- if (command == "cut" || command == "copy")
- {
- cin >> Fname;
- if (command == "cut")
- cut(Fname);
- else
- copy(Fname);
- }
- else if (command == "show")
- {
- nowNode->show();
- }
- else if (command == "create")
- {
- cin >> Fname >> Fformat;
- nowNode->create(Fname, FormatConvert(Fformat));
- }
- else if (command == "pasta")
- {
- pasta();
- }
- else if (command == "move")
- {
- cin >> Fname;
- Move(Fname);
- }
- else if (command == "return")
- {
- back();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement