summaryrefslogtreecommitdiff
path: root/cpp/nesting_path.cc
blob: f7e306302aa9705aac7b33bc255872e61890c6a5 (plain)
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
// Google Code Jam 2020
#include<iostream>

using namespace std;

void findall(string & data, string to_search, string replace_str) {
    size_t pos = data.find(to_search);

    while(pos != string::npos) {
        data.replace(pos, to_search.size(), replace_str);
        pos = data.find(to_search, pos + replace_str.size());
    }
}


string get_s2(const string& s) {
    string s2{};
    int index{};
    bool toa = true;
    for(int i = 0; i < s.length(); ++i) {    
        int n = (s[i]-'0');
        if(toa) {
            for(int j = 0; j < n; ++j) {
                s2.insert(index++, "(");
            }
        }
        s2 += (n+'0');
        ++index;
        if(s[i+1] != s[i]) {
            toa = true;
            for(int j = 0; j < n; ++j) {
                s2.insert(index++, ")");
            }
        } else {
            toa = false;
        }
    }

    while(s2.find(")(") != string::npos || s2.find("()") != string::npos) {
        findall(s2, ")(", "");
        findall(s2, "()", "");
    }

    return s2;
}

int main() {
    int N;
    cin >> N;

    for(int _ = 0; _ < N; ++_) {
        string s;
        cin >> s;

        cout << "Case #" << _+1 << ": " << get_s2(s) << endl;
    }
    return 0;
}