Problem p01308 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e10, EPS = 1e-9;

int n, m, t[50000], s[50000];
int so[256];

int tonum(char *buf) { return so[buf[0]] + (buf[1] == '#'); }

bool bfs() {
  queue<pi> Q;
  Q.push(mp(0, 0));
  while (!Q.empty()) {
    int cur = Q.front().first, step = Q.front().second;
    Q.pop();

    if (step == m) {
      if (cur + 1 == n + 1 || cur + 2 == n + 1)
        return 1;
      continue;
    }

    if (cur + 1 <= n && t[cur] == s[step])
      Q.push(mp(cur + 1, step + 1));
    if (cur + 2 <= n && (t[cur + 1] + 1) % 12 == s[step])
      Q.push(mp(cur + 2, step + 1));
    if (cur - 1 > 0 && (t[cur - 2] + 11) % 12 == s[step])
      Q.push(mp(cur - 1, step + 1));
  }
  return 0;
}

int main() {
  so['C'] = 0;
  so['D'] = 2;
  so['E'] = 4;
  so['F'] = 5;
  so['G'] = 7;
  so['A'] = 9;
  so['B'] = 11;

  int cs;
  scanf("%d", &cs);
  while (cs--) {
    scanf("%d%d", &n, &m);

    char buf[3];
    rep(i, n) scanf("%s", buf), t[i] = tonum(buf);
    rep(i, m) scanf("%s", buf), s[i] = tonum(buf);

    cout << (bfs() ? "Yes" : "No") << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e10, EPS = 1e-9;

int n, m, t[50000], s[50000];
int so[256];

int tonum(char *buf) { return so[buf[0]] + (buf[1] == '#'); }

bool bfs() {
  queue<pi> Q;
  Q.push(mp(0, 0));
  while (!Q.empty()) {
    int cur = Q.front().first, step = Q.front().second;
    Q.pop();

    if (n + 1 - cur > 2 * (m - step + 1))
      continue;

    if (step == m) {
      if (cur + 1 == n + 1 || cur + 2 == n + 1)
        return 1;
      continue;
    }

    if (cur + 1 <= n && t[cur] == s[step])
      Q.push(mp(cur + 1, step + 1));
    if (cur + 2 <= n && (t[cur + 1] + 1) % 12 == s[step])
      Q.push(mp(cur + 2, step + 1));
    if (cur - 1 > 0 && (t[cur - 2] + 11) % 12 == s[step])
      Q.push(mp(cur - 1, step + 1));
  }
  return 0;
}

int main() {
  so['C'] = 0;
  so['D'] = 2;
  so['E'] = 4;
  so['F'] = 5;
  so['G'] = 7;
  so['A'] = 9;
  so['B'] = 11;

  int cs;
  scanf("%d", &cs);
  while (cs--) {
    scanf("%d%d", &n, &m);

    char buf[3];
    rep(i, n) scanf("%s", buf), t[i] = tonum(buf);
    rep(i, m) scanf("%s", buf), s[i] = tonum(buf);

    cout << (bfs() ? "Yes" : "No") << endl;
  }
  return 0;
}