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

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int T;
  cin >> T;
  map<string, int> ms;
  vector<string> vs(
      {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"});
  for (int i = 0; i < 12; i++)
    ms[vs[i]] = i;
  while (T--) {
    int n, m;
    cin >> n >> m;
    vector<int> t(n + 1, 0), s(m);
    for (int i = 0; i < n; i++) {
      string x;
      cin >> x;
      t[i + 1] = ms[x];
    }
    for (int i = 0; i < m; i++) {
      string x;
      cin >> x;
      s[i] = ms[x];
    }
    if (0) {
      for (int i : s)
        cout << i << " ";
      cout << endl;
      for (int i : t)
        cout << i << " ";
      cout << endl;
    }

    auto check = [&](int x, int i) { return x + (m - i) * 2 >= n + 1; };

    if (n <= 100) {
      bool flg = 0;
      vector<int> dp(1, 0);
      for (int i = 0; i <= m; i++) {
        vector<int> nx;
        for (int x : dp) {
          if (i == m) {
            if (x + 2 >= n + 1) {
              flg = 1;
              break;
            }
          } else {
            if (x + 1 <= n && (t[x + 1] == s[i]))
              if (check(x + 1, i))
                nx.emplace_back(x + 1);
            if (x + 2 <= n && (t[x + 2] == (s[i] + 11) % 12))
              if (check(x + 2, i))
                nx.emplace_back(x + 2);
            if (x - 1 >= 0 && (t[x - 1] == (s[i] + 1) % 12))
              if (check(x - 1, i))
                nx.emplace_back(x - 1);
          }
        }
        sort(nx.begin(), nx.end());
        nx.erase(unique(nx.begin(), nx.end()), nx.end());
        if (0) {
          cout << i << ":" << endl;
          for (int x : nx)
            cout << x << " ";
          cout << endl;
        }
        swap(dp, nx);
        if (dp.empty())
          break;
      }
      cout << (flg ? "Yes" : "No") << endl;
      continue;
    }

    bool flg = 0;
    int h = n / 2;
    vector<int> ff, tt;
    {
      vector<int> dp(1, 0);
      for (int i = 0; i < h; i++) {
        vector<int> nx;
        for (int x : dp) {
          if (x + 1 <= n && (t[x + 1] == s[i]))
            if (check(x + 1, i))
              nx.emplace_back(x + 1);
          if (x + 2 <= n && (t[x + 2] == (s[i] + 11) % 12))
            if (check(x + 2, i))
              nx.emplace_back(x + 2);
          if (x - 1 >= 0 && (t[x - 1] == (s[i] + 1) % 12))
            if (check(x - 1, i))
              nx.emplace_back(x - 1);
        }
        sort(nx.begin(), nx.end());
        nx.erase(unique(nx.begin(), nx.end()), nx.end());
        swap(dp, nx);
        if (dp.empty())
          break;
      }
      ff = dp;
    }
    {
      vector<int> dp(1, n + 1);
      for (int i = m; i >= h; i--) {
        vector<int> nx;
        for (int x : dp) {
          if (x - 1 >= 0 && (i == m || t[x] == s[i]))
            nx.emplace_back(x - 1);
          if (x - 2 >= 0 && (i == m || t[x] == (s[i] + 11) % 12))
            nx.emplace_back(x - 2);
          if (x + 1 <= n && (i == m || t[x] == (s[i] + 1) % 12))
            nx.emplace_back(x + 1);
        }
        sort(nx.begin(), nx.end());
        nx.erase(unique(nx.begin(), nx.end()), nx.end());
        swap(dp, nx);
        if (dp.empty())
          break;
      }
      tt = dp;
    }

    set<int> cnt;
    for (int i : ff)
      cnt.emplace(i);
    for (int i : tt)
      flg |= cnt.count(i);
    cout << (flg ? "Yes" : "No") << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int T;
  cin >> T;
  map<string, int> ms;
  vector<string> vs(
      {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"});
  for (int i = 0; i < 12; i++)
    ms[vs[i]] = i;
  while (T--) {
    int n, m;
    cin >> n >> m;
    vector<int> t(n + 1, 0), s(m);
    for (int i = 0; i < n; i++) {
      string x;
      cin >> x;
      t[i + 1] = ms[x];
    }
    for (int i = 0; i < m; i++) {
      string x;
      cin >> x;
      s[i] = ms[x];
    }
    if (0) {
      for (int i : s)
        cout << i << " ";
      cout << endl;
      for (int i : t)
        cout << i << " ";
      cout << endl;
    }

    auto check = [&](int x, int i) { return x + (m - i) * 2 >= n + 1; };

    bool flg = 0;
    vector<int> dp(1, 0);
    for (int i = 0; i <= m; i++) {
      vector<int> nx;
      for (int x : dp) {
        if (i == m) {
          if (x + 2 >= n + 1) {
            flg = 1;
            break;
          }
        } else {
          if (x + 1 <= n && (t[x + 1] == s[i]))
            if (check(x + 1, i))
              nx.emplace_back(x + 1);
          if (x + 2 <= n && (t[x + 2] == (s[i] + 11) % 12))
            if (check(x + 2, i))
              nx.emplace_back(x + 2);
          if (x - 1 >= 0 && (t[x - 1] == (s[i] + 1) % 12))
            if (check(x - 1, i))
              nx.emplace_back(x - 1);
        }
      }
      sort(nx.begin(), nx.end());
      nx.erase(unique(nx.begin(), nx.end()), nx.end());
      if (0) {
        cout << i << ":" << endl;
        for (int x : nx)
          cout << x << " ";
        cout << endl;
      }
      swap(dp, nx);
      if (dp.empty())
        break;
    }
    cout << (flg ? "Yes" : "No") << endl;
  }
  return 0;
}