-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path1166.cpp
More file actions
48 lines (44 loc) · 1.17 KB
/
1166.cpp
File metadata and controls
48 lines (44 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
__________________________________________________________________________________________________
class FileSystem {
vector<string> split(string& s,char c){
int n=s.size();
vector<string> res;
for(int i=0;i<n;i++){
if(s[i]==c) continue;
string t;
while(i<n&&s[i]!=c) t.push_back(s[i++]);
res.push_back(t);
}
return res;
}
public:
struct Node{
map<string, Node*> nxt;
int value;
Node(){}
Node(int value):value(value){}
};
Node* root;
FileSystem() {
root=new Node();
}
bool create(string path, int value) {
auto vs=split(path,'/');
Node* pos=root;
for(int i=0;i+1<(int)vs.size();i++)
if(pos) pos=pos->nxt[vs[i]];
if(!pos) return false;
pos->nxt[vs.back()]=new Node(value);
return true;
}
int get(string path) {
auto vs=split(path,'/');
Node* pos=root;
for(int i=0;i<(int)vs.size();i++)
if(pos) pos=pos->nxt[vs[i]];
if(!pos) return -1;
return pos->value;
}
};
__________________________________________________________________________________________________
__________________________________________________________________________________________________