首页 > 代码库 > TOJ 4077 Brother Kai wants to know the subdirectory 模拟

TOJ 4077 Brother Kai wants to know the subdirectory 模拟

http://acm.tju.edu.cn/toj/showp.php?pid=4077

前三发各种跪,重写了一遍才过,也不明白原因,感到水平拙计。

因为不同文件夹下可能有相同名字的子文件夹,所以map的key值需要再记录一下上级文件夹。

 1 #include<cstring> 2 #include<string> 3 #include<iostream> 4 #include<cstdio> 5 #include<map> 6 using namespace std; 7  8 map<pair<string, int>, int> M; 9 int cur, cnt;10 struct DOC{11     string sub[30];12     int size, fa;13     int find(string);14     void push(string);15     void pop(string);16 }doc[3000];17 int DOC::find(string s)18 {19     for (int i = 0; i < size; i++)20         if (sub[i] == s) return i;21     return -1;22 }23 void DOC::push(string s)24 {25     if (find(s) != -1) return;26     doc[cnt].fa = cur;27     doc[cnt].size = 0;28     M[make_pair(s, cur)] = cnt ++;29     sub[size] = s;30     int p = size++;31     string tmp;32     while(p > 0 && sub[p] + sub[p-1] < sub[p-1] + sub[p]){33         tmp = sub[p];34         sub[p] = sub[p-1];35         sub[p-1] = tmp;36         p--;37     }38 }39 void DOC::pop(string s)40 {41     int p = find(s);42     if (p == -1) return;43     size --;44     for (; p < size; p++)45         sub[p] = sub[p+1];46 }47 void init()48 {49     M.clear();50     cur = cnt = 0;51     doc[cur].size = doc[cur].fa = 0;52     M[make_pair("/", 0)] = cnt++;53     doc[cur].push("home");54     cur = 1;55     doc[cur].push("brotherkai");56     cur = 2;57 }58 int main()59 {60     init();61     string command;62     while(cin >> command)63     {64         if (command == "exit") break;65         if (command == "cd"){66             cin >> command;67             if (command == "/")68                 cur = 0;69             else if (command == "..")70                 cur = doc[cur].fa;71             else{72                 if (M.find(make_pair(command, cur)) != M.end())73                     cur = M[make_pair(command, cur)];74             }75         }76         else if (command == "rm"){77             cin >> command;78             cin >> command;79             doc[cur].pop(command);80         }81         else if (command == "ls"){82             for (int i = 0; i < doc[cur].size; i ++){83                 if (i != 0) cout <<  ;84                 cout << doc[cur].sub[i];85             }86             cout << endl;87         }88         else if (command == "mkdir"){89             cin >> command;90             doc[cur].push(command);91         }92     }93     return 0;94 }