From 3d8dca4e262d647da3069bb70215d7e68ee1525d Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sat, 4 Apr 2020 19:16:04 +0200 Subject: Google Code Jam - Nesting path --- cpp/nesting_path.cc | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 cpp/nesting_path.cc (limited to 'cpp/nesting_path.cc') 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 + +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; +} -- cgit v1.2.3-18-g5258