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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;
string S[] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
int N, M, SLEN = 12;
map<string, int> strtoi;
vector<int> v, vv;
bool f = false;

void dfs(int cnt, int now) {
  int n = v[now];
  if (cnt < M && now == 0)
    return;
  if (cnt >= M) {
    if (now == 0)
      f = true;
    return;
  }
  // cout <<cnt <<", " <<now <<", " <<S[n] <<endl;
  // cout <<"    " <<S[(vv[cnt] - 1 + SLEN) % SLEN] <<", " <<S[vv[cnt]] <<", "
  // <<S[(vv[cnt] + 1) % SLEN] <<endl;
  if (vv[cnt] == n && now - 1 >= 0)
    dfs(cnt + 1, now - 1);
  if ((vv[cnt] + 1) % SLEN == n && now + 1 < N + 1)
    dfs(cnt + 1, now + 1);
  if ((vv[cnt] - 1 + SLEN) % SLEN == n && now - 2 >= 0)
    dfs(cnt + 1, now - 2);
}

int main() {
  REP(i, SLEN) strtoi[S[i]] = i;
  int T;
  cin >> T;
  while (T--) {
    f = false;
    cin >> N >> M;
    v = vector<int>(N + 1);
    vv = vector<int>(M);
    FOR(i, 1, N + 1) {
      string s;
      cin >> s;
      v[i] = strtoi[s];
    }
    REP(i, M) {
      string s;
      cin >> s;
      vv[i] = strtoi[s];
    }
    REVERSE(vv);
    dfs(0, N);
    if (!f)
      dfs(0, N - 1);
    cout << (f ? "Yes" : "No") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;
string S[] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
int N, M, SLEN = 12;
map<string, int> strtoi;
vector<int> v, vv;
bool f;

bool check(int now, int pls, int tar) {
  int m = v[now + pls];
  if (pls == 2)
    ++m;
  if (pls == -1)
    --m;
  m = (m + SLEN) % SLEN;
  return m == tar;
}

void dfs(int now, int cnt) {
  if (f)
    return;
  if (cnt >= M) {
    if ((N + 1) - now > 2)
      return;
    f = 1;
  } else {
    FOR(pls, -1, 3) {
      if (now + pls <= 0 || now + pls > N + 1)
        continue;
      if (check(now, pls, vv[cnt]))
        dfs(now + pls, cnt + 1);
    }
  }
}

int main() {
  REP(i, SLEN) strtoi[S[i]] = i;
  int T;
  cin >> T;
  while (T--) {
    cin >> N >> M;
    f = false;
    v = vector<int>(N + 1);
    vv = vector<int>(M);
    FOR(i, 1, N + 1) {
      string s;
      cin >> s;
      v[i] = strtoi[s];
    }
    REP(i, M) {
      string s;
      cin >> s;
      vv[i] = strtoi[s];
    }
    dfs(0, 0);
    cout << (f ? "Yes" : "No") << endl;
  }
  return 0;
}