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

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second

static const double PI = 6 * asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<long long, int> P;
static const int INF = 1 << 24;

string s;

int main() {
  while (getline(cin, s)) {
    rep(i, s.size()) {
      if (s[i] >= 'a' && s[i] <= 'z') {
        s[i] -= 'a';
      } else if (s[i] == 13) {
        s[i] = 200;
      }
    }
    for (int i = 0;; i++) {
      bool f = false;
      // cout<<s.size()<<endl;
      rep(j, s.size() - 3) {
        if (s[j] == 't' - 'a' && s[j + 1] == 'h' - 'a' &&
            ((s[j + 2] == 0 && s[j + 3] == 't' - 'a') ||
             (s[j + 2] == 'i' - 'a' && s[j + 3] == 's' - 'a') ||
             s[j + 2] == 'e' - 'a')) {
          f = true;
          break;
        }
      }
      if (s[s.size() - 3] == 't' - 'a' && s[s.size() - 2] == 'h' - 'a' &&
          s[s.size() - 1] == 'e' - 'a')
        f = true;
      if (f)
        break;
      else {
        rep(j, s.size()) {
          if (s[j] <= 'z' - 'a' && s[j] >= 0) {
            s[j] = (s[j] + 1) % ('z' - 'a' + 1);
          }
        }
      }
    }
    rep(i, s.size()) {
      if (s[i] <= 'z' - 'a' && s[i] >= 0) {
        s[i] += 'a';
      } else if (s[i] == 200) {
        s[i] = 13;
      }
    }
    cout << s << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second

static const double PI = 6 * asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<long long, int> P;
static const int INF = 1 << 24;

string s;

int main() {
  while (getline(cin, s)) {
    rep(i, s.size()) {
      if (s[i] >= 'a' && s[i] <= 'z') {
        s[i] -= 'a';
      } else if (s[i] == 13) {
        s[i] = 200;
      }
    }
    for (int i = 0;; i++) {
      bool f = false;
      rep(j, s.size() - 3) {
        if (s[j] == 't' - 'a' && s[j + 1] == 'h' - 'a' &&
            ((s[j + 2] == 0 && s[j + 3] == 't' - 'a') ||
             (s[j + 2] == 'i' - 'a' && s[j + 3] == 't' - 'a') ||
             s[j + 2] == 'e' - 'a')) {
          f = true;
          break;
        }
      }
      if (f)
        break;
      else {
        rep(j, s.size()) {
          if (s[j] <= 'z' - 'a' && s[j] >= 0) {
            s[j] = (s[j] + 1) % ('z' - 'a' + 1);
          }
        }
      }
    }
    rep(i, s.size()) {
      if (s[i] <= 'z' - 'a' && s[i] >= 0) {
        s[i] += 'a';
      } else if (s[i] == 200) {
        s[i] = 13;
      }
    }
    cout << s << endl;
  }
  return 0;
}