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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int N, M;
int step[50001];
int song[50001];
string note[] = {"C",  "C#", "D",  "D#", "E",  "F",
                 "F#", "G",  "G#", "A",  "A#", "B"};
int getidx(string s) {
  REP(i, 12) if (s == note[i]) return i;
  return -1;
}
bool dfs(int p, int idx) {
  if (idx == M)
    return (p == N - 1 || p == N - 2);
  int d[3] = {-1, 1, 2};
  int dif[3] = {-1, 0, 1};
  REP(r, 3) {
    int np = p + d[r];
    if (np >= 0 && np < N && (step[np] + dif[r] + 12) % 12 == song[idx] &&
        dfs(np, idx + 1)) {
      return true;
    }
  }
  return false;
}
bool dfs2(int p, int idx) {
  if (idx == -1)
    return (p == -1);
  if (p >= N || p < 0)
    return false;
  int dif = (song[idx] - step[p]);
  if (dif == 11)
    dif = -1;
  if (dif == -11)
    dif = +1;
  if (abs(dif) >= 2)
    return false;
  if (dif == 0)
    return dfs2(p - 1, idx - 1);
  if (dif == 1)
    return dfs2(p - 2, idx - 1);
  if (dif == -1)
    return dfs2(p + 1, idx - 1);
  assert(false);
}

int main() {
  int T;
  cin >> T;
  while (T--) {
    cin >> N >> M;
    REP(i, N) {
      string s;
      cin >> s;
      step[i] = getidx(s);
      assert(step[i] != -1);
    }
    REP(i, M) {
      string s;
      cin >> s;
      song[i] = getidx(s);
    }
    // bool ans = dfs(-1, 0);
    bool ans2 = (dfs2(N - 1, M - 1) || dfs2(N - 2, M - 1));
    if (ans2)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int N, M;
int step[50001];
int song[50001];
string note[] = {"C",  "C#", "D",  "D#", "E",  "F",
                 "F#", "G",  "G#", "A",  "A#", "B"};
int getidx(string s) {
  REP(i, 12) if (s == note[i]) return i;
  return -1;
}
bool dfs(int p, int idx) {
  if (idx == M)
    return (p == N - 1 || p == N - 2);
  int d[3] = {-1, 1, 2};
  int dif[3] = {-1, 0, 1};
  REP(r, 3) {
    int np = p + d[r];
    if (np >= 0 && np < N && (step[np] + dif[r] + 12) % 12 == song[idx] &&
        dfs(np, idx + 1)) {
      return true;
    }
  }
  return false;
}

int main() {
  int T;
  cin >> T;
  while (T--) {
    cin >> N >> M;
    REP(i, N) {
      string s;
      cin >> s;
      step[i] = getidx(s);
      assert(step[i] != -1);
    }
    REP(i, M) {
      string s;
      cin >> s;
      song[i] = getidx(s);
    }
    bool ans = dfs(-1, 0);
    if (ans)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}