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

#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;
typedef pair<string, string> pss;
typedef pair<int, int> pii;
typedef pair<pair<pss, pss>, pair<pii, char>>
    pssc; // red:green:bottom:hidari:down's count:slide's count:center

string red, green, bottom, h;
char center;

void down() {
  if (center != 0) {
    bottom += center;
    center = 0;
  }
  if (red.size() > 0) {
    center = red[0];
    red.erase(red.begin());
  }
}

void slide() {
  if (center != 0) {
    h += center;
    center = 0;
  }
  if (green.size() > 0) {
    center = green[0];
    green.erase(green.begin());
  }
}

int main() {
  string ans, ansbottom;
  while (1) {
    cin >> red;
    if (red == "-")
      break;
    cin >> green >> ansbottom;
    h = "";
    int limitd = red.size();
    int limits = green.size() + 1;
    center = 0;
    bottom = "";
    slide();
    pssc now, alt;
    now.first.first.first = red;
    now.first.first.second = green;
    now.first.second.first = bottom;
    now.first.second.second = h;
    now.second.second = center;
    now.second.first.first = 0;
    now.second.first.second = 1;
    queue<pssc> que;
    que.push(now);
    while (!que.empty()) {
      alt = now = que.front();
      que.pop();
      red = now.first.first.first;
      green = now.first.first.second;
      bottom = now.first.second.first;
      h = now.first.second.second;
      center = now.second.second;
      if (now.second.first.first > limitd)
        continue;
      if (now.second.first.second > limits)
        continue;
      bool check = false;
      rep(i, bottom.size()) {
        if (bottom[i] != ansbottom[i]) {
          check = true;
          break;
        }
      }
      if (check)
        continue;
      if (red.size() == 0 && green.size() == 0 && center == 0) {
        if (ansbottom == bottom) {
          ans = h;
          break;
        }
      }
      if (red.size() > 0) {
        down();
        now.first.first.first = red;
        now.first.first.second = green;
        now.first.second.first = bottom;
        now.first.second.second = h;
        now.second.second = center;
        now.second.first.first++;
        que.push(now);
      } else if (center != 0) {
        down();
        now.first.first.first = red;
        now.first.first.second = green;
        now.first.second.first = bottom;
        now.first.second.second = h;
        now.second.second = center;
        now.second.first.first++;
        que.push(now);
      }
      now = alt;
      red = now.first.first.first;
      green = now.first.first.second;
      bottom = now.first.second.first;
      h = now.first.second.second;
      center = now.second.second;
      if (green.size() > 0) {
        slide();
        now.first.first.first = red;
        now.first.first.second = green;
        now.first.second.first = bottom;
        now.first.second.second = h;
        now.second.second = center;
        now.second.first.second++;
        que.push(now);
      } else if (center != 0) {
        slide();
        now.first.first.first = red;
        now.first.first.second = green;
        now.first.second.first = bottom;
        now.first.second.second = h;
        now.second.second = center;
        now.second.first.second++;
        que.push(now);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;
typedef pair<string, string> pss;
typedef pair<pair<pss, pss>, char> pssc; // red:green:bottom:hidari:center

string red, green, bottom, h;
char center;

void down() {
  if (center != 0) {
    bottom += center;
    center = 0;
  }
  if (red.size() > 0) {
    center = red[0];
    red.erase(red.begin());
  }
}

void slide() {
  if (center != 0) {
    h += center;
    center = 0;
  }
  if (green.size() > 0) {
    center = green[0];
    green.erase(green.begin());
  }
}

int main() {
  string ans, ansbottom;
  while (cin >> red, red != "-") {
    cin >> green >> ansbottom;
    h = "";
    center = 0;
    pssc now, alt;
    now.first.first.first = red;
    now.first.first.second = green;
    now.second = 0;
    now.first.second.first = now.first.second.second = "";
    queue<pssc> que;
    que.push(now);
    while (!que.empty()) {
      alt = now = que.front();
      que.pop();
      red = now.first.first.first;
      green = now.first.first.second;
      bottom = now.first.second.first;
      h = now.first.second.second;
      center = now.second;
      if (red.size() == 0 && green.size() == 0 && center == 0) {
        if (ansbottom == bottom) {
          ans = h;
          break;
        }
      }
      if (red.size() > 0) {
        down();
        now.first.first.first = red;
        now.first.first.second = green;
        now.first.second.first = bottom;
        now.first.second.second = h;
        now.second = center;
        que.push(now);
      } else if (center != 0) {
        down();
        now.first.first.first = red;
        now.first.first.second = green;
        now.first.second.first = bottom;
        now.first.second.second = h;
        now.second = center;
        que.push(now);
      }
      now = alt;
      red = now.first.first.first;
      green = now.first.first.second;
      bottom = now.first.second.first;
      h = now.first.second.second;
      center = now.second;
      if (green.size() > 0) {
        slide();
        now.first.first.first = red;
        now.first.first.second = green;
        now.first.second.first = bottom;
        now.first.second.second = h;
        now.second = center;
        que.push(now);
      } else if (center != 0) {
        slide();
        now.first.first.first = red;
        now.first.first.second = green;
        now.first.second.first = bottom;
        now.first.second.second = h;
        now.second = center;
        que.push(now);
      }
    }
    cout << ans << endl;
  }
  return 0;
}