Click here to Skip to main content
14,332,423 members

C / C++ / MFC

 
GeneralRe: error C2059 Pin
Richard MacCutchan21-Aug-19 3:59
protectorRichard MacCutchan21-Aug-19 3:59 
GeneralRe: error C2059 Pin
_Flaviu21-Aug-19 20:50
member_Flaviu21-Aug-19 20:50 
GeneralRe: error C2059 Pin
Stefan_Lang21-Aug-19 5:24
memberStefan_Lang21-Aug-19 5:24 
GeneralRe: error C2059 Pin
_Flaviu21-Aug-19 21:45
member_Flaviu21-Aug-19 21:45 
GeneralRe: error C2059 Pin
Stefan_Lang21-Aug-19 21:51
memberStefan_Lang21-Aug-19 21:51 
GeneralRe: error C2059 Pin
_Flaviu21-Aug-19 23:53
member_Flaviu21-Aug-19 23:53 
AnswerRe: error C2059 Pin
Stefan_Lang21-Aug-19 3:08
memberStefan_Lang21-Aug-19 3:08 
QuestionC++ Program to decompress a compressed string Pin
antoniu20016-Aug-19 6:19
memberantoniu20016-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 toRepeat;
    }
    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.

AnswerRe: C++ Program to decompress a compressed string Pin
Richard MacCutchan17-Aug-19 1:19
protectorRichard MacCutchan17-Aug-19 1:19 
GeneralRe: C++ Program to decompress a compressed string Pin
antoniu20017-Aug-19 1:39
memberantoniu20017-Aug-19 1:39 
GeneralRe: C++ Program to decompress a compressed string Pin
Richard MacCutchan17-Aug-19 3:02
protectorRichard MacCutchan17-Aug-19 3:02 
GeneralRe: C++ Program to decompress a compressed string Pin
antoniu20017-Aug-19 3:12
memberantoniu20017-Aug-19 3:12 
GeneralRe: C++ Program to decompress a compressed string Pin
Richard MacCutchan17-Aug-19 3:22
protectorRichard MacCutchan17-Aug-19 3:22 
GeneralRe: C++ Program to decompress a compressed string Pin
antoniu20017-Aug-19 3:24
memberantoniu20017-Aug-19 3:24 
GeneralRe: C++ Program to decompress a compressed string Pin
Richard MacCutchan17-Aug-19 3:59
protectorRichard MacCutchan17-Aug-19 3:59 
GeneralRe: C++ Program to decompress a compressed string Pin
antoniu20017-Aug-19 4:02
memberantoniu20017-Aug-19 4:02 
GeneralRe: C++ Program to decompress a compressed string Pin
Richard MacCutchan17-Aug-19 4:19
protectorRichard MacCutchan17-Aug-19 4:19 
GeneralRe: C++ Program to decompress a compressed string Pin
David Crow19-Aug-19 4:49
memberDavid Crow19-Aug-19 4:49 
AnswerRe: C++ Program to decompress a compressed string Pin
Patrice T17-Aug-19 19:16
mvePatrice T17-Aug-19 19:16 
GeneralRe: C++ Program to decompress a compressed string Pin
antoniu20018-Aug-19 2:42
memberantoniu20018-Aug-19 2:42 
AnswerRe: C++ Program to decompress a compressed string Pin
Randor 18-Aug-19 12:49
professional Randor 18-Aug-19 12:49 
GeneralRe: C++ Program to decompress a compressed string Pin
antoniu20018-Aug-19 23:05
memberantoniu20018-Aug-19 23:05 
GeneralRe: C++ Program to decompress a compressed string Pin
Randor 18-Aug-19 23:17
professional Randor 18-Aug-19 23:17 
GeneralRe: C++ Program to decompress a compressed string Pin
antoniu20018-Aug-19 23:27
memberantoniu20018-Aug-19 23:27 
GeneralRe: C++ Program to decompress a compressed string Pin
Randor 18-Aug-19 23:42
professional Randor 18-Aug-19 23:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.