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

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

#define f first
#define s second

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); 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()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int t[50010];
int s[50010];

int n, m;

#define OTO(x, y) (((x) + 12 + (y)) % 12)

bool loop(int i, int j) {
  if (i == 0 && j == -1)
    return true;
  if (i == n + 1)
    return false;
  if (j == -1)
    return false;
  if (i < 0)
    return false;

  int tt = t[i];
  int ss = s[j];

  if (OTO(tt, 0) == ss)
    return loop(i - 1, j - 1);
  if (OTO(tt, 1) == ss)
    return loop(i - 2, j - 1);
  if (OTO(tt, -1) == ss)
    return loop(i + 1, j - 1);

  return false;
}

int main() {
  int cc;
  map<string, int> mp;
  mp["C"] = 0;
  mp["C#"] = 1;
  mp["D"] = 2;
  mp["D#"] = 3;
  mp["E"] = 4;
  mp["F"] = 5;
  mp["F#"] = 6;
  mp["G"] = 7;
  mp["G#"] = 8;
  mp["A"] = 9;
  mp["A#"] = 10;
  mp["B"] = 11;

  scanf("%d", &cc);

  while (cc-- > 0) {
    char buff[8];

    scanf("%d%d", &n, &m);

    REP(i, n) {
      scanf("%s", buff);
      t[i + 1] = mp[buff];
    }
    REP(i, m) {
      scanf("%s", buff);
      s[i] = mp[buff];
    }

    puts((loop(n, m - 1) || loop(n - 1, m - 1)) ? "Yes" : "No");
  }

  return 0;
}

Error: TLE

Fixed submission:

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

#define f first
#define s second

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); 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()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int t[50010];
int s[50010];
set<pair<int, int>> memo;

int n, m;

#define OTO(x, y) (((x) + 12 + (y)) % 12)

bool dfs(int i, int j) {
  if (i >= n - 1 && j == m)
    return true;
  if (j == m)
    return false;
  int next = s[j];

  if (memo.count(make_pair(i, j)) != 0)
    return false;
  memo.insert(make_pair(i, j));

  if (t[i + 1] == next && dfs(i + 1, j + 1))
    return true;
  if (i + 2 <= n && OTO(t[i + 2], 1) == next && dfs(i + 2, j + 1))
    return true;
  if (i > 1 && OTO(t[i - 1], -1) == next && dfs(i - 1, j + 1))
    return true;

  return false;
}

int main() {
  int cc;
  map<string, int> mp;
  mp["C"] = 0;
  mp["C#"] = 1;
  mp["D"] = 2;
  mp["D#"] = 3;
  mp["E"] = 4;
  mp["F"] = 5;
  mp["F#"] = 6;
  mp["G"] = 7;
  mp["G#"] = 8;
  mp["A"] = 9;
  mp["A#"] = 10;
  mp["B"] = 11;

  scanf("%d", &cc);

  while (cc-- > 0) {
    char buff[8];

    memo.clear();
    scanf("%d%d", &n, &m);

    REP(i, n) {
      scanf("%s", buff);
      t[i + 1] = mp[buff];
    }
    REP(i, m) {
      scanf("%s", buff);
      s[i] = mp[buff];
    }

    puts(dfs(0, 0) ? "Yes" : "No");
  }

  return 0;
}