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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int n, m;
vi a, b;
bool f(int s, int t) {
  if (t == -1) {
    if (s == -1)
      return true;
    return false;
  }
  if (s > n || s < 0)
    return false;
  if (a[s] == b[t] && f(s - 1, t - 1))
    return true;
  if ((a[s] + 1) % 12 == b[t] && f(s - 2, t - 1))
    return true;
  if ((a[s] + 11) % 12 == b[t] && f(s + 1, t - 1))
    return true;
  return false;
}
int main() {
  map<string, int> M;
  M["A"] = 0;
  M["A#"] = 1;
  M["B"] = 2;
  M["C"] = 3;
  M["C#"] = 4;
  M["D"] = 5;
  M["D#"] = 6;
  M["E"] = 7;
  M["F"] = 8;
  M["F#"] = 9;
  M["G"] = 10;
  M["G#"] = 11;
  int q;
  cin >> q;
  while (q--) {
    cin >> n >> m;
    a = vi(n);
    b = vi(m);
    rep(i, n) {
      string s;
      cin >> s;
      a[i] = M[s];
    }
    rep(i, m) {
      string s;
      cin >> s;
      b[i] = M[s];
    }
    if (f(n - 1, m - 1) || n != 1 && f(n - 2, m - 1))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int n, m;
vi a, b;
bool f(int s, int t) {
  if (t == m) {
    if (n - 2 <= s)
      return true;
    return false;
  }
  int r = s + 1;
  if (r != n) {
    if (b[t] == a[r] && f(r, t + 1))
      return true;
  }
  r = s + 2;
  if (r < n) {
    if (b[t] == (a[r] + 1) % 12 && f(r, t + 1))
      return true;
  }
  r = s - 1;
  if (r >= 0) {
    if (b[t] == (a[r] + 11) % 12 && f(r, t + 1))
      return true;
  }
  return false;
}
int main() {
  map<string, int> M;
  M["A"] = 0;
  M["A#"] = 1;
  M["B"] = 2;
  M["C"] = 3;
  M["C#"] = 4;
  M["D"] = 5;
  M["D#"] = 6;
  M["E"] = 7;
  M["F"] = 8;
  M["F#"] = 9;
  M["G"] = 10;
  M["G#"] = 11;
  int q;
  cin >> q;
  while (q--) {
    cin >> n >> m;
    a = vi(n);
    b = vi(m);
    rep(i, n) {
      string s;
      cin >> s;
      a[i] = M[s];
    }
    rep(i, m) {
      string s;
      cin >> s;
      b[i] = M[s];
    }
    if (f(-1, 0))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}