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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <sstream>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define EPS (1e-15)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;

typedef long long ll;

bool LT(long double a, long double b) { return !equals(a, b) && a < b; }
bool LTE(long double a, long double b) { return equals(a, b) || a < b; }

double target;

/*
  void sternBrocot(long long pl = 0, long long ql = 1, long long pr = 1, long
  long qr = 0) { long long pm = pl + pr, qm = ql + qr; long double v = pm /
  (long double)qm; if( equals(v,0.0) ) return; if( LT(target,v) )
  sternBrocot(pl, ql, pm, qm); if( equals(v,target) ) { cout << pm << "/" << qm
  << endl; return;
  }
  if( LT(v,target) ) sternBrocot(pm, qm, pr, qr);
  }
*/
struct Data {
  long long pl, ql, pr, qr;
};

void sternBrocot() {
  deque<Data> deq;
  deq.push_back((Data){0, 1, 1, 0});
  while (!deq.empty()) {
    Data data = deq.back();
    deq.pop_back();
    long long pm = data.pl + data.pr, qm = data.ql + data.qr;
    long double v = pm / (long double)qm;
    if (equals(v, 0.0))
      continue;
    if (LT(target, v)) {
      deq.push_back((Data){data.pl, data.ql, pm, qm});
    }
    if (equals(v, target)) {
      cout << pm << "/" << qm << endl;
      break;
    }
    if (LT(v, target)) {
      deq.push_back((Data){pm, qm, data.pr, data.qr});
    }
  }
}

int main() {
  string s;
  cin >> s;
  if (s.find("(") == string::npos) {
    int cnt = 0;
    rep(i, (int)s.size()) {
      if (s[(int)s.size() - 1 - i] == '.')
        break;
      ++cnt;
    }
    ll denom = 1, frac = 0;
    rep(i, cnt) denom *= 10LL;
    rep(i, (int)s.size()) {
      char c = s[i];
      if (c == '.')
        continue;
      frac *= 10;
      frac += (s[i] - '0');
    }
    ll gcd = __gcd(frac, denom);
    frac /= gcd;
    denom /= gcd;
    cout << frac << "/" << denom << endl;
  } else {
    string suffix = "";
    bool f = false;
    rep(i, (int)s.size()) {
      if (s[i] == ')')
        f = false;
      if (f)
        suffix += s[i];
      if (s[i] == '(')
        f = true;
    }
    string prefix = "";
    rep(i, (int)s.size()) {
      if (s[i] == '(')
        break;
      prefix += s[i];
    }
    s = prefix;
    rep(i, 30) s += suffix;
    stringstream ss;
    ss << s;
    ss >> target;
    sternBrocot();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <sstream>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)
#define EPS (1e-12)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;

bool LT(long double a, long double b) { return !equals(a, b) && a < b; }
bool LTE(long double a, long double b) { return equals(a, b) || a < b; }

double target;

/*
void sternBrocot(long long pl = 0, long long ql = 1, long long pr = 1, long long
qr = 0) { long long pm = pl + pr, qm = ql + qr; long double v = pm / (long
double)qm; if( equals(v,0.0) ) return; if( LT(target,v) ) sternBrocot(pl, ql,
pm, qm); if( equals(v,target) ) { cout << pm << "/" << qm << endl; return;
  }
  if( LT(v,target) ) sternBrocot(pm, qm, pr, qr);
}
*/
struct Data {
  long long pl, ql, pr, qr;
};

void sternBrocot() {
  deque<Data> deq;
  deq.push_back((Data){0, 1, 1, 0});
  while (!deq.empty()) {
    Data data = deq.back();
    deq.pop_back();
    long long pm = data.pl + data.pr, qm = data.ql + data.qr;
    long double v = pm / (long double)qm;
    if (equals(v, 0.0))
      continue;
    if (LT(target, v)) {
      deq.push_back((Data){data.pl, data.ql, pm, qm});
    }
    if (equals(v, target)) {
      cout << pm << "/" << qm << endl;
      break;
    }
    if (LT(v, target)) {
      deq.push_back((Data){pm, qm, data.pr, data.qr});
    }
  }
}

int main() {
  string s;
  cin >> s;
  if (s.find("(") != string::npos) {
    string suffix = "";
    bool f = false;
    rep(i, (int)s.size()) {
      if (s[i] == ')')
        f = false;
      if (f)
        suffix += s[i];
      if (s[i] == '(')
        f = true;
    }
    string prefix = "";
    rep(i, (int)s.size()) {
      if (s[i] == '(')
        break;
      prefix += s[i];
    }
    s = prefix;
    rep(i, 15) s += suffix;
  }
  stringstream ss;
  ss << s;
  ss >> target;
  sternBrocot();
  return 0;
}