diff options
author | Santo Cariotti <dcariotti24@gmail.com> | 2020-04-04 19:16:04 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-04 19:16:04 +0200 |
commit | 3d8dca4e262d647da3069bb70215d7e68ee1525d (patch) | |
tree | 65395a74f96214fbf4fc699b756b77ea4688d6b1 | |
parent | 6dbfe789e8f92d4519599151bf56ea70ecfb178b (diff) |
Google Code Jam - Nesting path
-rw-r--r-- | cpp/nesting_path.cc | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/cpp/nesting_path.cc b/cpp/nesting_path.cc new file mode 100644 index 0000000..f7e3063 --- /dev/null +++ b/cpp/nesting_path.cc @@ -0,0 +1,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; +} |