Problem p03081 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>

using namespace std;

int func(int start, string s, char *t, char *d, int Q, int N);
int main(void) {
  int N;
  cin >> N;
  int Q;
  cin >> Q;
  string s;
  cin >> s;
  int min;
  int max;
  char t[Q];
  char d[Q];
  for (int i = 0; i < Q; i++) {
    cin >> t[i];
    cin >> d[i];
  }
  int A;
  A = 0;
  int B;
  B = N - 1;
  int C;
  for (int k = 0; k < N; k++) {

    if (func(0, s, t, d, Q, N) >= 0) {
      min = 0;
      break;
    }
    if (func(N - 1, s, t, d, Q, N) < 0) {
      min = N;
      break;
    }
    C = (A + B) / 2;

    if (func(C, s, t, d, Q, N) < 0) {
      A = C;
    } else {
      B = C;
    }
    if (A + 1 == B && func(A, s, t, d, Q, N) < 0 &&
        func(B, s, t, d, Q, N) >= 0) {
      min = B;
      break;
    }
  }
  A = 0;
  B = N - 1;
  for (int l = 0; l < N; l++) {
    if (func(N - 1, s, t, d, Q, N) < N) {
      max = N - 1;
      break;
    }
    if (func(0, s, t, d, Q, N) > N) {
      max = -1;
      break;
    }
    C = (A + B) / 2;
    if (func(C, s, t, d, Q, N) >= N) {
      B = C;
    } else {
      A = C;
    }
    if (A + 1 == B && func(A, s, t, d, Q, N) < N &&
        func(B, s, t, d, Q, N) >= N) {
      max = A;
      break;
    }
  }

  cout << max - min + 1 << endl;
}
int func(int start, string s, char *t, char *d, int Q, int N) {
  for (int i = 0; i < Q; i++) {
    if (i > 0) {
      ++t;
      ++d;
    }
    if (*t == s[start]) {
      if (*d == 'R') {
        start++;
      } else {
        start = start - 1;
      }
      if (start < 0 || start >= N) {
        return start;
        break;
      }
    }
  }

  return start;
}

Fixed submission:
#include <iostream>
#include <string>

using namespace std;

int func(int start, string s, char *t, char *d, int Q, int N);
int main(void) {
  int N;
  cin >> N;
  int Q;
  cin >> Q;
  string s;
  cin >> s;
  int min;
  int max;
  char t[Q];
  char d[Q];
  for (int i = 0; i < Q; i++) {
    cin >> t[i];
    cin >> d[i];
  }
  int A;
  A = 0;
  int B;
  B = N - 1;
  int C;
  for (int k = 0; k < N; k++) {

    if (func(0, s, t, d, Q, N) >= 0) {
      min = 0;
      break;
    }
    if (func(N - 1, s, t, d, Q, N) < 0) {
      min = N;
      break;
    }
    C = (A + B) / 2;

    if (func(C, s, t, d, Q, N) < 0) {
      A = C;
    } else {
      B = C;
    }
    if (A + 1 == B && func(A, s, t, d, Q, N) < 0 &&
        func(B, s, t, d, Q, N) >= 0) {
      min = B;
      break;
    }
  }
  A = 0;
  B = N - 1;
  for (int l = 0; l < N; l++) {
    if (func(N - 1, s, t, d, Q, N) < N) {
      max = N - 1;
      break;
    }
    if (func(0, s, t, d, Q, N) >= N) {
      max = -1;
      break;
    }
    C = (A + B) / 2;
    if (func(C, s, t, d, Q, N) >= N) {
      B = C;
    } else {
      A = C;
    }
    if (A + 1 == B && func(A, s, t, d, Q, N) < N &&
        func(B, s, t, d, Q, N) >= N) {
      max = A;
      break;
    }
  }

  cout << max - min + 1 << endl;
}
int func(int start, string s, char *t, char *d, int Q, int N) {
  for (int i = 0; i < Q; i++) {
    if (i > 0) {
      ++t;
      ++d;
    }
    if (*t == s[start]) {
      if (*d == 'R') {
        start++;
      } else {
        start = start - 1;
      }
      if (start < 0 || start >= N) {
        return start;
        break;
      }
    }
  }

  return start;
}