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

#include <climits>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

const string note[] = {"C",  "C#", "D",  "D#", "E",  "F",
                       "F#", "G",  "G#", "A",  "A#", "B"};

int main() {
  int N;
  cin >> N;
  // N = 1;

  while (N--) {
    int n, m;
    cin >> n >> m;

    vector<int> stairs;
    vector<int> music;
    for (int i = 0; i < n; ++i) {
      char buf[256];
      scanf(" %s", buf);
      for (int j = 0; j < 12; ++j) {
        if (note[j] == buf) {
          stairs.push_back(j);
          break;
        }
      }
    }
    for (int i = 0; i < m; ++i) {
      char buf[256];
      scanf(" %s", buf);
      for (int j = 0; j < 12; ++j) {
        if (note[j] == buf) {
          music.push_back(j);
          break;
        }
      }
    }

    bool ok = false;
    for (int start = n - 2; start <= n - 1; ++start) {
      if (start < 0)
        continue;
      int mpos = m - 1;
      int pos = start;
      for (; mpos >= 0; --mpos) {
        if (stairs[pos] == music[mpos]) {
          if (--pos < 0)
            goto next;
        } else if ((stairs[pos] + 1) % 12 == music[mpos]) {
          pos -= 2;
          if (pos < 0)
            goto next;
        } else if ((stairs[pos] + 11) % 12 == music[mpos]) {
          pos += 1;
          if (pos >= n)
            goto next;
        } else
          break;
      }
    next:
      if (mpos <= 0 && pos == -1) {
        ok = true;
        break;
      }
    }
    cout << (ok ? "Yes" : "No") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <climits>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

const string note[] = {"C",  "C#", "D",  "D#", "E",  "F",
                       "F#", "G",  "G#", "A",  "A#", "B"};

int main() {
  int N;
  cin >> N;

  while (N--) {
    int n, m;
    cin >> n >> m;

    vector<int> stairs;
    vector<int> music;
    for (int i = 0; i < n; ++i) {
      char buf[256];
      scanf(" %s", buf);
      for (int j = 0; j < 12; ++j) {
        if (note[j] == buf) {
          stairs.push_back(j);
          break;
        }
      }
    }
    for (int i = 0; i < m; ++i) {
      char buf[256];
      scanf(" %s", buf);
      for (int j = 0; j < 12; ++j) {
        if (note[j] == buf) {
          music.push_back(j);
          break;
        }
      }
    }

    vector<int> cache(n, 0);
    vector<bool> half(n, false);
    priority_queue<pair<int, int>> q;
    q.push(make_pair(1, -1));

    while (!q.empty()) {
      int to_play = -q.top().first + 1;
      int pos = q.top().second;
      q.pop();

      if (to_play == m / 2) {
        half[pos] = true;
        // cout << pos << endl;
        continue;
      }

      if (pos - 1 >= 0) {
        if ((stairs[pos - 1] + 11) % 12 == music[to_play]) {
          if (to_play + 1 > cache[pos - 1]) {
            cache[pos - 1] = to_play + 1;
            q.push(make_pair(-to_play, pos - 1));
          }
        }
      }
      if (pos + 2 < n) {
        if ((stairs[pos + 2] + 1) % 12 == music[to_play]) {
          if (to_play + 1 > cache[pos + 2]) {
            cache[pos + 2] = to_play + 1;
            q.push(make_pair(-to_play, pos + 2));
          }
        }
      }
      if (pos + 1 < n) {
        if (stairs[pos + 1] == music[to_play]) {
          if (to_play + 1 > cache[pos + 1]) {
            cache[pos + 1] = to_play + 1;
            q.push(make_pair(-to_play, pos + 1));
          }
        }
      }
    }

    fill(cache.begin(), cache.end(), INT_MAX);
    q.push(make_pair(m - 1, n - 1));
    q.push(make_pair(m - 1, n - 2));
    bool ok = false;
    while (!q.empty()) {
      int to_play = q.top().first;
      int pos = q.top().second;
      q.pop();

      if (to_play == m / 2 - 1) {
        // cout << pos << endl;
        if (half[pos]) {
          ok = true;
          break;
        } else
          continue;
      }
      if (pos - 1 >= 0) {
        if (stairs[pos] == music[to_play]) {
          if (to_play - 1 < cache[pos]) {
            cache[pos] = to_play - 1;
            q.push(make_pair(to_play - 1, pos - 1));
          }
        }
      }
      if (pos - 2 >= 0) {
        if ((stairs[pos] + 1) % 12 == music[to_play]) {
          if (to_play - 1 < cache[pos]) {
            cache[pos] = to_play - 1;
            q.push(make_pair(to_play - 1, pos - 2));
          }
        }
      }
      if (pos + 1 < n) {
        if ((stairs[pos] + 11) % 12 == music[to_play]) {
          if (to_play - 1 < cache[pos]) {
            cache[pos] = to_play - 1;
            q.push(make_pair(to_play - 1, pos + 1));
          }
        }
      }
    }
    cout << (ok ? "Yes" : "No") << endl;
  }
  return 0;
}