Skip to content

Instantly share code, notes, and snippets.

@likejazz
Last active March 31, 2024 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save likejazz/848b9d226f7c0c4237515a4aea43cd87 to your computer and use it in GitHub Desktop.
Save likejazz/848b9d226f7c0c4237515a4aea43cd87 to your computer and use it in GitHub Desktop.
Long Encoded String
#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