Problem p01696 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

char str[801];
int temp;

string delque(const string &);
string revstr(const string &);
string Cipher();
string String();
char Letter();

int main() {
  while (1) {
    temp = 0;
    fgets(str, 801, stdin);
    if (str[0] == '.' && str[1] == '\\n')
      break;
    cout << delque(Cipher()) << endl;
  }
  return 0;
}

string delque(const string &instr) {
  string retst;
  char st[801];
  strcpy(st, instr.c_str());
  for (int i = 0; st[i] != '\\0'; i++) {
    if (st[i] == '?')
      st[i] = 'A';
  }
  retst = st;
  return retst;
}

string revstr(const string &instr) {
  int i;
  string retstr;
  char st[801], ret[801];
  strcpy(st, instr.c_str());
  for (i = 0; st[i] != '\\0'; i++) {
  }
  i--;
  for (; i >= 0; i--) {
    retstr += st[i];
  }
  return retstr;
}

string Cipher() {
  string ret = String();
  while (1) {
    if (str[temp] == '\\n') {
      break;
    } else if (str[temp] == ']') {
      break;
    } else {
      ret += String();
    }
  }
  return ret;
}

string String() {
  int i;
  string ret;
  char rev[801];
  if (str[temp] == '[') {
    temp++;
    ret += revstr(Cipher());
    if (str[temp] == ']') {
      temp++;
    }
  } else {
    ret += Letter();
  }
  return ret;
}

char Letter() {
  char ret;
  if (str[temp] == '+') {
    temp++;
    ret = Letter() + 1;
    if (ret == '?' + 1) {
      ret--;
    } else if (ret > 'Z') {
      ret -= 'Z' - 'A' + 1;
    }
  } else if (str[temp] == '-') {
    temp++;
    ret = Letter() - 1;
    if (ret == '?' - 1) {
      ret++;
    } else if (ret < 'A') {
      ret += 'Z' - 'A' + 1;
    }
  } else if ((str[temp] >= 'A' && str[temp] <= 'Z') || str[temp] == '?') {
    ret = str[temp++];
  }
  return ret;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

char str[81];
int temp;

string delque(const string &);
string revstr(const string &);
string Cipher();
string String();
char Letter();

int main() {
  while (1) {
    temp = 0;
    fgets(str, 81, stdin);
    if (str[0] == '.' && str[1] == '\\n')
      break;
    cout << delque(Cipher()) << endl;
  }
  return 0;
}

string delque(const string &instr) {
  string retst;
  char st[81];
  strcpy(st, instr.c_str());
  for (int i = 0; st[i] != '\\0'; i++) {
    if (st[i] == '?')
      st[i] = 'A';
  }
  retst = st;
  return retst;
}

string revstr(const string &instr) {
  int i;
  string retstr;
  char st[81], ret[81];
  strcpy(st, instr.c_str());
  for (i = 0; st[i] != '\\0'; i++) {
  }
  i--;
  for (; i >= 0; i--) {
    retstr += st[i];
  }
  return retstr;
}

string Cipher() {
  string ret = String();
  while (1) {
    if (str[temp] == '\\n') {
      break;
    } else if (str[temp] == ']') {
      break;
    } else {
      ret += String();
    }
  }
  return ret;
}

string String() {
  int i;
  string ret;
  char rev[81];
  if (str[temp] == '[') {
    temp++;
    ret += revstr(Cipher());
    if (str[temp] == ']') {
      temp++;
    }
  } else {
    ret += Letter();
  }
  return ret;
}

char Letter() {
  char ret;
  if (str[temp] == '+') {
    temp++;
    ret = Letter() + 1;
    if (ret == '?' + 1) {
      ret--;
    } else if (ret > 'Z') {
      ret -= 'Z' - 'A' + 1;
    }
  } else if (str[temp] == '-') {
    temp++;
    ret = Letter() - 1;
    if (ret == '?' - 1) {
      ret++;
    } else if (ret < 'A') {
      ret += 'Z' - 'A' + 1;
    }
  } else if ((str[temp] >= 'A' && str[temp] <= 'Z') || str[temp] == '?') {
    ret = str[temp++];
  }
  return ret;
}