Last active
March 31, 2024 13:38
-
-
Save likejazz/848b9d226f7c0c4237515a4aea43cd87 to your computer and use it in GitHub Desktop.
Long Encoded String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <regex> | |
using namespace std; | |
int alphabet[26]; | |
void tokenized(int a, int c) { | |
alphabet[a - 1] += c; | |
} | |
int main() { | |
// inputs for encoded-string | |
string s; | |
cin >> s; | |
// check valid string | |
regex r("[0-9#()]*"); | |
if (!(regex_match(s.begin(), s.end(), r))) { | |
cout << "Error" << endl; | |
return 0; | |
} | |
// insert meaningless char for comparison conveniently. | |
s[s.size()] = '-'; | |
s[s.size()] = '-'; | |
s[s.size()] = '-'; | |
// tokenizing | |
int a = 0, c = 0; | |
bool c_tag_opened = false; | |
for (int i = 0; i < s.size(); i++) { | |
switch (s[i]) { | |
case '(': | |
c_tag_opened = true; | |
break; | |
case ')': | |
c_tag_opened = false; | |
tokenized(a, c); | |
c = 0; | |
break; | |
default: | |
if (c_tag_opened) { | |
c = c * 10 + (s[i] - '0'); // be careful, max count is 99 | |
} else { | |
if (s[i + 2] != '#') { // single num | |
a = s[i] - '0'; // https://stackoverflow.com/a/5030086/3513266 | |
if (s[i + 1] != '(') { | |
tokenized(a, 1); | |
} | |
} else if (s[i + 2] == '#') { // double num | |
a = (s[i] - '0') * 10 + (s[i + 1] - '0'); | |
if (s[i + 3] != '(') { | |
tokenized(a, 1); | |
} | |
i += 2; // skip double number | |
} | |
} | |
break; | |
} | |
} | |
// print an array | |
for (int i = 0; i < 26; i++) | |
cout << alphabet[i] << " "; | |
cout << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment