題目連結

一題很讚的設計題。

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
#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;
}
();

// handle special cases first
// [], "", ...
// range of input?
class Codec
{
public:
    // Encodes a list of strings to a single string.
    string encode(vector<string> &strs)
    {
        string ans;
        for (auto i : strs)
            ans += to_string(i.length()) + ":" + i;
        return ans;
    }

    // Decodes a single string to a list of strings.
    vector<string> decode(string str)
    {
        vector<string> ans;
        for (int i = 0; i < (int)str.length(); i++) {
            int s = i, e = -1;
            for (int j = s; j < (int)str.length(); j++) {
                if (str[j] == ':') {
                    e = j;
                    break;
                }
            }

            // cout << "substr: " << str.substr(s, e - s) << endl;
            int len = stoi(str.substr(s, e - s));
            ans.push_back(str.substr(e + 1, len));
            i = e + len;
        }
        return ans;
    }
};

// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.decode(codec.encode(strs));

#ifdef LOCAL
int main()
{
    Codec codec;
    vector<string> inp{"abc", "abcd"};
    codec.decode(codec.encode(inp));
    return 0;
}
#endif