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

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define M_PI 3.141592
#define toRad 2.0 * M_PI / 360.0
#define inin(x)                                                                \\
  int x;                                                                       \\
  cin >> x;
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " " << x << endl;
int euclid(int a, int b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  else
    return euclid(b, a % b);
}
char hoge(int a) { return 'a' + a; }
bool check(string a) {
  if (a.find("that", 0) != string::npos || a.find("this", 0) != string::npos) {
    return true;
  }
  return false;
}
int main() {
  int n;
  cin >> n;
  cin.ignore();
  while (n--) {
    string str;
    bool ok = true;
    getline(cin, str);
    for (int a = 0; ok; a++) {
      if (euclid(a, 26) != 1)
        continue;
      for (int b = 0; ok && b < 26; b++) {
        string ans = "";
        for (int i = 0; i < str.size(); i++) {
          if (str[i] == ' ') {
            ans += " ";
            continue;
          }
          for (int y = 0; y < 26; y++) {
            int num = (a * y + b) % 26;
            if (hoge(num) == str[i]) {
              ans += hoge(y);
              break;
            }
          }
        }
        if (check(ans)) {
          cout << ans << endl;
          ok = false;
        }
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define M_PI 3.141592
#define toRad 2.0 * M_PI / 360.0
#define inin(x)                                                                \\
  int x;                                                                       \\
  cin >> x;
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " " << x << endl;
int euclid(int a, int b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  else
    return euclid(b, a % b);
}
char hoge(int a) { return 'a' + a; }
bool check(string a) {
  if (a.find("that", 0) != string::npos || a.find("this", 0) != string::npos) {
    return true;
  }
  return false;
}
int main() {
  int n;
  cin >> n;
  cin.ignore();
  while (n--) {
    string a;
    bool ok = true;
    getline(cin, a);
    cin.ignore();
    cout << a << endl;
    for (int i = 0; ok; i++) {
      if (euclid(i, 26) != 1)
        continue;
      for (int l = 0; l <= 25 && ok; l++) {
        for (int k = 0; k <= 25 && ok; k++) {
          int j = (i * l + k) % 26;
          if (hoge(j) == a[0]) {
            string ans = "";
            for (int o = 0; o < a.size(); o++) {
              if ('a' <= a[o] && a[o] <= 'z') {
                for (int u = 0; u <= 25; u++) {
                  int h = i * u + k;
                  h %= 26;
                  if (hoge(h) == a[o]) {
                    ans += hoge(u);
                    break;
                  }
                }
              } else {
                ans += " ";
              }
            }
            if (check(ans)) {
              cout << ans << endl;
              ok = false;
            }
          }
        }
      }
    }
  }
}