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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define all(c) ((c).begin()), ((c).end())
#define debug(c) cerr << "> " << #c << " = " << (c) << endl;
#define iter(c) __typeof((c).begin())
#define present(c, e) ((c).find((e)) != (c).end())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)

#define mp make_pair
#define fst first
#define snd second
#define pb push_back

const double EPS = 1e-10;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef complex<double> P;

int main() {
  int n;
  cin >> n;
  getchar();
  while (n--) {
    string s;
    getline(cin, s);
    rep(a, 26) rep(b, 26) {
      string t = s;
      rep(i, t.size()) if (isalpha(t[i])) t[i] =
          (a * (t[i] - 'a') + b) % 26 + 'a';
      if (t.find("that") != -1 || t.find("this") != -1) {
        cout << t << endl;
        goto next;
      }
    }
  next:;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define all(c) ((c).begin()), ((c).end())
#define debug(c) cerr << "> " << #c << " = " << (c) << endl;
#define iter(c) __typeof((c).begin())
#define present(c, e) ((c).find((e)) != (c).end())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)

#define mp make_pair
#define fst first
#define snd second
#define pb push_back

const double EPS = 1e-10;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef complex<double> P;

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }

int main() {
  int n;
  cin >> n;
  getchar();
  while (n--) {
    string s;
    getline(cin, s);
    int ca[26], cb[26];
    memset(ca, 0, sizeof(ca));
    rep(a, 1000000000) if (gcd(a, 26) == 1) if (ca[a % 26] == 0) {
      ca[a % 26] = 1;
      int cnt = 26;
      memset(cb, 0, sizeof(cb));
      rep(b, 1000000000) {
        if (cb[b % 26])
          continue;
        cnt--;
        string t = s;
        rep(i, t.size()) if (isalpha(t[i])) t[i] =
            (a * (t[i] - 'a') + b) % 26 + 'a';
        if (t.find("that") != -1 || t.find("that") != -1) {
          cout << t << endl;
          goto next;
        }
        cb[b % 26] = 1;
        if (!cnt)
          break;
      }
    }
  next:;
  }

  return 0;
}