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

#include <bits/stdc++.h>
using namespace std;

void foo(int t) {
  static int s = 0;
  if (s++)
    cout << " ";
  cout << t / 60 << ":";
  if (t % 60 <= 9)
    cout << "0" << t % 60;
  else
    cout << t % 60;
}

int main() {
  int N, M, h[110], k[110];
  int i = 0, j = 0, l = 0, s = 0;
  int a, b, temp = -1;

  cin >> N;
  for (i = 0; i < N; i++) {
    cin >> a >> b;
    h[i] = a * 60 + b;
  }
  cin >> M;
  for (i = 0; i < M; i++) {
    cin >> a >> b;
    k[i] = a * 60 + b;
  }

  while (j < N || l < M) {
    if (j == N) {
      if (temp != k[l]) {
        temp = k[l];
        foo(temp);
      }
      l++;
    } else if (l == M) {
      if (temp != h[j]) {
        temp = h[j];
        foo(temp);
      }
      j++;
    } else {
      if (h[j] < k[l]) {
        if (temp != h[j]) {
          temp = h[j];
          foo(temp);
        }
        j++;
      } else if (h[j] > k[l]) {
        if (temp != k[l]) {
          temp = k[l];
          foo(temp);
        }
        l++;
      } else if (h[j] == k[l]) {
        if (temp != h[j]) {
          temp = h[j];
          foo(temp);
        }
        j++;
        l++;
      }
    }
  }
  cout << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int foo(string a) {
  int i, size = (int)a.size();

  for (i = 0; i < size; i++) {
    if (!('0' <= a[i] && a[i] <= '9'))
      return 0;
  }
  // cout<<"DD "<<a<<endl;
  return 1;
}

int huga(int pl[], int count, int m) {
  int k = 1;
  while (k <= m) {
    if (count == m)
      count = 1;
    else
      count++;
    if (pl[count] != -1)
      return count;
    k++;
  }

  return -1;
}

int hoge(string str, int i) {
  int keta = log10(i) + 1, size = (int)str.size(), an = 0, k, j = 1;

  if (keta != size)
    return 0;
  for (k = size - 1; k >= 0; k--) {
    an += (str[k] - '0') * j;
    j *= 10;
  } // cout<<str<<" "<<i<<" "<<an<<" "<<endl;
  if (an == i)
    return 1;
  else
    return 0;
}

int main() {
  string str[10010];
  int m, n, temp;
  int i, j, k;

  while (1) {
    int count = 1, pl[1010] = {};
    cin >> m >> n;
    if (m == 0 && n == 0)
      break;

    for (i = 1; i <= n; i++) {
      cin >> str[i];
    }

    for (i = 1; i <= n; i++) {
      if (i % 5 == 0 && i % 3 == 0) {
        if (str[i] != "FizzBuzz") {
          pl[count] = -1;
        }
      } else if (i % 5 == 0) {
        if (str[i] != "Buzz") {
          pl[count] = -1;
        }
      } else if (i % 3 == 0) {
        if (str[i] != "Fizz") {
          pl[count] = -1;
        }
      } else {
        //	cout<<"AAA "<<i<<endl;
        //	cout<<foo(str[i])<<"   "<<str[i]<<endl;
        if (foo(str[i]) == 1) {
          if (hoge(str[i], i) == 0) {
            pl[count] = -1;
          }
        } else {
          pl[count] = -1;
        }
      }
      //  temp = count;
      // count = huga(pl, count , m);
      // for(int s = 1; s <= m; s++)cout<<"S "<<s<<" "<<pl[s]<<endl;

      //  cout<<"count "<<count<<" "<<huga(pl, count , m)<<endl;
      count = huga(pl, count, m);
      if (count == -1) {
        break;
      }
    }

    k = 0;

    for (i = 1; i <= m; i++) {
      if (pl[i] != -1) {
        if (k) {
          cout << " ";
        }
        cout << i;
        k++;
      }
    }
    cout << endl;
  }

  return 0;
}