Paul <
peps...@gmail.com> wrote in
news:11bd72d2-fdbf-4447...@googlegroups.com:
> I am writing code to compress string representations by representing
> (for example) "aaa" by a3.
So, how can you tell later if the original string was "aaa" or "a3"? Oh,
I see, it's always a char and a count, so "abc" gets "compressed" to
"a1b1c1". OK, fine, but how do you uncompress "a111b5"? It might be
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbb" or "a1bbbbb".
> The details of the problem, together with
> my solution, are provided below. I'm a bit concerned about my Append
> functions. The reason I wrote them this way, rather than simply using
> the normal library function, append, is that push_back guarantees
> amortized constant time (I think), meaning that my version of append
> is linear in the length of the appended word. In contrast to this, I
> couldn't find any guarantee that standard append is any better than
> being linear in the length of the concatenated word. Is this
> reasoning sound? Many thanks for your help.
Regardless of standardise, your Append() is most probably slower than
library string append. Your Append() is pushing each char separately,
meaning that for each char std::string needs to check for free buffer
space and update the string length field. Standard append does these
things only once for the appended string.
But as always with optimizations, there is no other way to find out the
real truth than to measure it. Please post back your speed testing
results here, it would be interesting to know such things.
>
> Paul
>
> #include <string>
> // Implementing a method to perform basic string compression using the
> counts // of repeated characters. For example, the string aabcccccaaa
> would become // a2b1c5a3. If the compressed string would not become
> smaller than the original // string, return the original string. You
> can assume the string has only upper // and lower case letters (a -
> z).
>
> void Append(string& str1, const string& str2)
> {
> for(auto c: str2)
> str1.push_back(c);
> }
>
> void Append(string& str1, int x)
> {
> Append(str1, to_string(x));
> }
>
> string compression(string input)
If you are concerned about speed, this should be const string& input.
> {
> const int inputSize = input.length();
>
> if(inputSize <= 2)
> return input;
>
> string results;
> int repeated = 1; // Counting occurrences of each character
>
> for(int i = 1; i < inputSize; ++i)
> if(input[i] == input[i-1])
> ++repeated;
> else
> {
> results.push_back(input[i-1]);
> Append(results, repeated);
> repeated = 1;
> }
>
> results.push_back(input[inputSize - 1]);
> Append(results, repeated);
>
> return results.length() < inputSize ? results : input;
Oh, I see you still cannot tell if "a3" should be uncompressed to "aaa"
or "a3" ;-)
>
> }
>
Cheers
Paavo