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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

map<string, int> sound;
int n, m;
int stairs[50001];
int song[50001];

bool simulate(int s) {
  for (int i = 0; i < m; i++) {
    if (i == m - 1) {
      int ns = s - 1;
      if (ns == -1 &&
          (stairs[s] + sound.size()) % sound.size() == song[m - 1 - i]) {
        return true;
      }
      ns = s - 2;
      if (ns == -1 &&
          (stairs[s] + 1 + sound.size()) % sound.size() == song[m - 1 - i]) {
        return true;
      }
      return false;
    }
    int ns = s + 1;
    if (ns < n && ns >= 0 &&
        (stairs[s] - 1 + sound.size()) % sound.size() == song[m - 1 - i]) {
      s = ns;
      continue;
    }
    ns = s - 1;
    if (ns < n && ns >= 0 &&
        (stairs[s] + sound.size()) % sound.size() == song[m - 1 - i]) {
      s = ns;
      continue;
    }
    ns = s - 2;
    if (ns < n && ns >= 0 &&
        (stairs[s] + 1 + sound.size()) % sound.size() == song[m - 1 - i]) {
      s = ns;
      continue;
    }
    return false;
  }
}

int main() {

  sound["C"] = 0;
  sound["C#"] = 1;
  sound["D"] = 2;
  sound["D#"] = 3;
  sound["E"] = 4;
  sound["F"] = 5;
  sound["F#"] = 6;
  sound["G"] = 7;
  sound["G#"] = 8;
  sound["A"] = 9;
  sound["A#"] = 10;
  sound["B"] = 11;
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      stairs[i] = sound[s];
    }
    for (int i = 0; i < m; i++) {
      string s;
      cin >> s;
      song[i] = sound[s];
    }
    if (simulate(n - 1) || simulate(n - 2))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

map<string, int> sound;
int n, m;
int stairs[50001];
int song[50001];
bool passed[50001];

bool bfs() {
  queue<int> q[2];
  int cur = 0;
  int nxt = 1;
  q[cur].push(-1);
  int step = 0;
  while (q[cur].size()) {
    memset(passed, 0, sizeof(passed));
    while (q[cur].size()) {
      int a = q[cur].front();
      q[cur].pop();
      int na = a + 1;
      if (na >= 0 && na < n && song[step] == stairs[na] && !passed[na] &&
          (m - step) * 2 >= n - na) {
        q[nxt].push(na);
        passed[na] = true;
      }
      na = a + 2;
      if (na >= 0 && na < n && (stairs[na] + 1) % sound.size() == song[step] &&
          !passed[na] && (m - step) * 2 >= n - na) {
        q[nxt].push(na);
        passed[na] = true;
      }
      na = a - 1;
      if (na >= 0 && na < n &&
          (stairs[na] - 1 + sound.size()) % sound.size() == song[step] &&
          !passed[na] && (m - step) * 2 >= n - na) {
        q[nxt].push(na);
        passed[na] = true;
      }
    }
    swap(cur, nxt);
    step++;
    if (step == m) {
      if (passed[n - 1] || passed[n - 2])
        return true;
      return false;
    }
  }
  return false;
}

int main() {

  sound["C"] = 0;
  sound["C#"] = 1;
  sound["D"] = 2;
  sound["D#"] = 3;
  sound["E"] = 4;
  sound["F"] = 5;
  sound["F#"] = 6;
  sound["G"] = 7;
  sound["G#"] = 8;
  sound["A"] = 9;
  sound["A#"] = 10;
  sound["B"] = 11;
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      stairs[i] = sound[s];
    }
    for (int i = 0; i < m; i++) {
      string s;
      cin >> s;
      song[i] = sound[s];
    }
    if (bfs())
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }

  return 0;
}