Last active
August 29, 2015 14:03
-
-
Save Onaiplee/6fce459f4b70f97095c8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
void encode_word(string &s) { | |
encode_word_helper(s, 0); | |
} | |
void encode_word_helper(string &s, int i) { | |
if (i == s.size()) { | |
cout << reduce(s) << endl; | |
return; | |
} | |
char c = s[i]; | |
encode_word_helper(s, i + 1); | |
s[i] = '1'; | |
encode_word_helper(s, i + 1); | |
s[i] = c; | |
} | |
string reduce (const string &s) { | |
string ret; | |
int i = 0; | |
while (i < s.size()) { | |
if (s[i] == '1') { | |
int cnt = 0; | |
for (; i < s.size(); i++) { | |
if (s[i] == '1') { | |
cnt++; | |
} else { | |
break; | |
} | |
} | |
ret += to_string(cnt); | |
} else { | |
ret.push_back(s[i++]); | |
} | |
} | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment