模拟题,map搞一搞。要想清楚一个结点应该是要通过一个字符串找到下一个结点,题目保证所以文件夹非空,所以只要判断一个结点是不是叶子结点就可以判断它是不是文件,用了点c11的特性。
#include<bits/stdc++.h> using namespace std; typedef map<string,int> Node; map<string,int>::iterator it_id; const int maxnd = 1e4; Node nds[maxnd]; int nds_cnt; #define MP make_pair #define fi first #define se second inline int id(const string& s,Node& fa) { if((it_id = fa.find(s)) != fa.end()) return it_id->second; fa.insert(MP(s,++nds_cnt)); return nds_cnt; } int curFile,curFolder; void dfs(int u) { for(auto it: nds[u]){ if(nds[it.se].empty()) { curFile++; continue; } else curFolder++; dfs(it.se); } } #define cer(x) cout<<x<<endl; #define PB push_back int main() { //freopen("in.txt","r",stdin); ios_base::sync_with_stdio(false); string s; while(cin>>s){ auto u = nds; u = nds+id(s.substr(0,1),*u); s.PB('\\'); for(int i = 3, j = 3; i < (int)s.size(); i++){ if(s[i] == '\\'){ u = nds+id(s.substr(j,i-j),*u); j = i+1; } } } int maxFolder = 0,maxFile = 0; for(auto it: nds[0]){ for(auto it2: nds[it.se]){ curFolder = curFile = 0; dfs(it2.se); maxFolder = max(maxFolder,curFolder); maxFile = max(maxFile,curFile); } } cout<<maxFolder<<' '<<maxFile<<endl; return 0; }