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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

int T[50000];
int S[50000];

int main() {
  string hoge[] = {"C",  "C#", "D",  "D#", "E",  "F",
                   "F#", "G",  "G#", "A",  "A#", "B"};
  map<string, int> mp;
  REP(i, 12) mp[hoge[i]] = i;
  int NUM;
  cin >> NUM;
  while (NUM--) {
    int n, m;
    cin >> n >> m;
    REP(i, n) {
      string s;
      cin >> s;
      T[i] = mp[s];
    }
    REP(i, m) {
      string s;
      cin >> s;
      S[i] = mp[s];
    }
    queue<pii> Q;
    Q.push(pii(-1, -1));
    bool ok = 0;
    while (!Q.empty()) {
      pii p = Q.front();
      Q.pop();
      int a = p.first, b = p.second;

      int rest = n - a;
      if ((m - b) * 2 < n - a)
        continue;
      if (a == n) {
        if (b == m) {
          ok = 1;
          break;
        }
        continue;
      }
      if (a + 1 <= n) {
        if (a + 1 == n || T[a + 1] == S[b + 1]) {
          Q.push(pii(a + 1, b + 1));
        }
      }
      if (a + 2 <= n) {
        if (a + 2 == n || (T[a + 2] + 1) % 12 == S[b + 1]) {
          Q.push(pii(a + 2, b + 1));
        }
      }
      if (a) {
        if ((T[a - 1] + 11) % 12 == S[b + 1]) {
          Q.push(pii(a - 1, b + 1));
        }
      }
    }
    cout << (ok ? "Yes" : "No") << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

int T[50000];
int S[50000];

int main() {
  string hoge[] = {"C",  "C#", "D",  "D#", "E",  "F",
                   "F#", "G",  "G#", "A",  "A#", "B"};
  map<string, int> mp;
  REP(i, 12) mp[hoge[i]] = i;
  int NUM;
  cin >> NUM;
  while (NUM--) {
    int n, m;
    cin >> n >> m;
    REP(i, n) {
      string s;
      cin >> s;
      T[i] = mp[s];
    }
    REP(i, m) {
      string s;
      cin >> s;
      S[i] = mp[s];
    }
    queue<pii> Q;
    Q.push(pii(-1, -1));
    bool ok = 0;
    map<pii, bool> visited;
    while (!Q.empty()) {
      pii p = Q.front();
      Q.pop();
      int a = p.first, b = p.second;
      // cout << a << " " << b << endl;
      if (visited[pii(a, b)])
        continue;
      visited[pii(a, b)] = 1;
      if (a == n) {
        if (b == m) {
          ok = 1;
          break;
        }
        continue;
      }
      if (a + 1 <= n) {
        if (a + 1 == n || T[a + 1] == S[b + 1]) {
          Q.push(pii(a + 1, b + 1));
        }
      }
      if (a + 2 <= n) {
        if (a + 2 == n || (T[a + 2] + 1) % 12 == S[b + 1]) {
          Q.push(pii(a + 2, b + 1));
        }
      }
      if (a) {
        if ((T[a - 1] + 11) % 12 == S[b + 1]) {
          Q.push(pii(a - 1, b + 1));
        }
      }
    }
    cout << (ok ? "Yes" : "No") << endl;
  }
}