題目連結

如何有效率的拆解並分析字串呢?請善用stack!

AC Code

 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifdef LOCAL
#include <bits/stdc++.h>
using namespace std;

// tree node stuff here...
#endif

static int __initialSetup = []()
{
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}
();

class Solution
{
public:
    int lengthLongestPath(string input)
    {
        stack<int> st; // length so far

        int ans = 0;

        string tok;
        stringstream ss;
        ss.str(input);

        while (getline(ss, tok, '\n')) {
            // get \t -> nesting level
            int level = -1;
            for (int i = (int)tok.size(); i >= 0; i--) {
                if (tok[i] == '\t') {
                    level = i;
                    break;
                }
            }
            level++; // pos 0 has \t -> 1 \t -> nesting level 1

            while (level < (int)st.size()) {
                st.pop();
            }

            // stack top length + / + (current name - tab count)
            int len = (st.size() > 0 ? st.top() + 1 : 0) + ((int)tok.size() - level);
            if (tok.find(".") != string::npos)
                ans = max(ans, len);
            else
                st.push(len);
        }

        return ans;
    }
};

#ifdef LOCAL
int main()
{
    cout << Solution().lengthLongestPath("dir\n\t    file.txt") << endl;
    cout << Solution().lengthLongestPath("dir\n        file.txt")
         << endl; // a filename as a top-level file WTF

    // cout << Solution().lengthLongestPath(
    //          "dir\n\tsubdir1\n\t\tfile1."
    //          "ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext")
    //      << endl;

    // cout << Solution().lengthLongestPath("a") << endl;
    return 0;
}
#endif