14,298,626 members

# C / C++ / MFC

 Re: error C2059 Richard MacCutchan21-Aug-19 3:59 Richard MacCutchan 21-Aug-19 3:59
 Re: error C2059 _Flaviu21-Aug-19 20:50 _Flaviu 21-Aug-19 20:50
 Re: error C2059 Stefan_Lang21-Aug-19 5:24 Stefan_Lang 21-Aug-19 5:24
 Re: error C2059 _Flaviu21-Aug-19 21:45 _Flaviu 21-Aug-19 21:45
 Re: error C2059 Stefan_Lang21-Aug-19 21:51 Stefan_Lang 21-Aug-19 21:51
 Re: error C2059 _Flaviu21-Aug-19 23:53 _Flaviu 21-Aug-19 23:53
 Re: error C2059 Stefan_Lang21-Aug-19 3:08 Stefan_Lang 21-Aug-19 3:08
 C++ Program to decompress a compressed string antoniu20016-Aug-19 6:19 antoniu200 16-Aug-19 6:19
Hello! So, I've encountered this problem, on the site I usually work on, that requires me to 'decompress' a 'compressed' string. The code I came up with gives 4 Correct Answers and 6 Wrong Answers. The problem goes like this:

Consider the pattern `n[string]`, which is equivalent to the succession `(string) ... (string)` (`(string)` repeated `n` times). Starting with this model, any string can be compressed.

For example:
• `1[a]` is equivalent to `a`
• `2[ab]` is equivalent to `abab`
• `2[a2[b]]` is equivalent to `abbabb`
• `3[b2[ca]]` is equivalent to `bcacabcacabcaca`
Requirement

Given a compressed string of characters, display its decompression.

Input data

The program reads from the keyboard a correctly compressed string `S` of characters.

Output data

The program will display a string of characters that will represent the decompression of the string `S`.

Restrictions and clarifications
• `3 ≤ the length of the string S ≤ 1000`
• `decompressed string length ≤ 100,000`
• `the string S will contain only lowercase letters of the English alphabet`
• `Time limit: 0.6 seconds`
• `Memory limit: 64 MB (global) / 8 MB(local)`
Example

### Input 1

`3[a1[b2[c]]]`

### Output 1

`abccabccabcc`

### Input 2

`3[a2[c]]2[x3[y]]`

### Output 2

`accaccaccxyyyxyyy`

My code:
```include <iostream>
include <string>
include <cctype>

string input;

int createNumber(int &pos) {
int number = 0;
while (isdigit(input[pos]))
number = number * 10 + input[pos] - '0', input.erase(input.begin() + pos);
return number;
}
string insideParanthesis(int &pos) {
if (input.length()) {
input.erase(input.begin() + pos);
string toRepeat = "";
while (input[pos] != ']') {
while (isalpha(input[pos]) && pos < input.length())
pos++, toRepeat += input[pos - 1];

if (isdigit(input[pos])) {
int timesExpr = createNumber(pos);
int posStart = pos;
timesExpr--;
string expr = insideParanthesis(pos);
if (timesExpr < 0) {
input.erase(input.begin() + posStart, input.begin() + pos);
continue;
}
while (timesExpr--)
input.insert(pos, expr), toRepeat += expr, pos += expr.length();
toRepeat += expr;
}
}
input.erase(input.begin() + pos);
}
return "";
}

int main() {
ios_base::sync_with_stdio(false);

getline(cin, input);

for (int i = 0; i < input.length(); i++) {
if (isdigit(input[i])) {
int timesExpr = createNumber(i);
int iStart = i;
timesExpr--;
string expr = insideParanthesis(i);
if (timesExpr < 0 && input.length()) {
input.erase(input.begin() + iStart, input.begin() + i);
continue;
}
else if (!(input.length()))
return 0;
while (timesExpr--)
input.insert(i, expr);
}
}

for (int i = 0; i < input.length(); i++) {
if (input[i] == '[' || input[i] == ']')
continue;
else
cout << input[i];
}
}```

I'd like to add that I've seen sumbitted sources on C++ with the same runtime as my source, but with 10 test cases correct out of 10.

If someone has any advice that could help me get those test cases right, I'd be thankful.

modified 19-Aug-19 5:03am.

 Re: C++ Program to decompress a compressed string Richard MacCutchan17-Aug-19 1:19 Richard MacCutchan 17-Aug-19 1:19
 Re: C++ Program to decompress a compressed string antoniu20017-Aug-19 1:39 antoniu200 17-Aug-19 1:39
 Re: C++ Program to decompress a compressed string Richard MacCutchan17-Aug-19 3:02 Richard MacCutchan 17-Aug-19 3:02
 Re: C++ Program to decompress a compressed string antoniu20017-Aug-19 3:12 antoniu200 17-Aug-19 3:12
 Re: C++ Program to decompress a compressed string Richard MacCutchan17-Aug-19 3:22 Richard MacCutchan 17-Aug-19 3:22
 Re: C++ Program to decompress a compressed string antoniu20017-Aug-19 3:24 antoniu200 17-Aug-19 3:24
 Re: C++ Program to decompress a compressed string Richard MacCutchan17-Aug-19 3:59 Richard MacCutchan 17-Aug-19 3:59
 Re: C++ Program to decompress a compressed string antoniu20017-Aug-19 4:02 antoniu200 17-Aug-19 4:02
 Re: C++ Program to decompress a compressed string Richard MacCutchan17-Aug-19 4:19 Richard MacCutchan 17-Aug-19 4:19
 Re: C++ Program to decompress a compressed string David Crow19-Aug-19 4:49 David Crow 19-Aug-19 4:49
 Re: C++ Program to decompress a compressed string Patrice T17-Aug-19 19:16 Patrice T 17-Aug-19 19:16
 Re: C++ Program to decompress a compressed string antoniu20018-Aug-19 2:42 antoniu200 18-Aug-19 2:42
 Re: C++ Program to decompress a compressed string `Randor` 18-Aug-19 12:49 `Randor` 18-Aug-19 12:49
 Re: C++ Program to decompress a compressed string antoniu20018-Aug-19 23:05 antoniu200 18-Aug-19 23:05
 Re: C++ Program to decompress a compressed string `Randor` 18-Aug-19 23:17 `Randor` 18-Aug-19 23:17
 Re: C++ Program to decompress a compressed string antoniu20018-Aug-19 23:27 antoniu200 18-Aug-19 23:27
 Re: C++ Program to decompress a compressed string `Randor` 18-Aug-19 23:42 `Randor` 18-Aug-19 23:42
 Last Visit: 16-Sep-19 10:06     Last Update: 16-Sep-19 10:06 Refresh « Prev1...3456789101112 Next »