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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
#define INF 1e+8
#define EPS 1e-7
#define PB push_back
#define fi first
#define se second
#define ll long long
#define MOD 26
#define reps(i, j, k) for (int i = (j); i < (k); i++)
#define rep(i, j) reps(i, 0, j)
typedef pair<int, int> Pii;
typedef vector<int> vi;
int main() {
  int n;
  scanf("%d%*c", &n);
  while (n--) {
    string str;
    getline(cin, str);
    vector<string> target;
    rep(i, str.size() - 3) {
      string sub(str, i, 4);
      if (i == 0) {
        if (str[4] == ' ' || str[4] == '\\0') {
          if (str[0] != ' ' && str[1] != ' ' && str[2] != ' ' &&
              str[3] != ' ') {
            target.PB(sub);
          }
        }
      } else if (i == str.size() - 4) {
        if (str[str.size() - 5] == ' ') {
          if (str[i] != ' ' && str[i + 1] != ' ' && str[i + 2] != ' ' &&
              str[i + 3] != ' ') {
            target.PB(sub);
          }
        }
      } else {
        if (str[i - 1] == ' ' && str[i + 4] == ' ') {
          if (str[i] != ' ' && str[i + 1] != ' ' && str[i + 2] != ' ' &&
              str[i + 3] != ' ') {
            target.PB(sub);
          }
        }
      }
    }
    string en = "abcdefghijklmnopqrstuvwxyz";
    string ans = "";
    int i = 0;
    int alpha;
    int beta;
    while (++i) {
      if (i % 2 == 0 || i % 13 == 0)
        continue;
      rep(j, 26) {
        rep(k, target.size()) {
          string sub;
          rep(l, 4) { sub += en[((i * (target[k][l] - 'a') + j) % MOD)]; }
          if (sub == "this" || sub == "that") {
            alpha = i;
            beta = j;
            goto end;
          }
        }
      }
    }
  end:;
    rep(i, str.size()) {
      if (str[i] == ' ')
        ans += " ";
      else
        ans += en[((alpha * (str[i] - 'a') + beta) % MOD)];
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
#define INF 1e+8
#define EPS 1e-7
#define PB push_back
#define fi first
#define se second
#define ll long long
#define MOD 26
#define reps(i, j, k) for (int i = (j); i < (k); i++)
#define rep(i, j) reps(i, 0, j)
typedef pair<int, int> Pii;
typedef vector<int> vi;
int main() {
  int n;
  scanf("%d", &n);
  while (n--) {
    char str[258];
    getchar();
    fgets(str, 258, stdin);
    set<string> target;
    stringstream stre(str);
    string tmp;
    while (stre >> tmp) {
      if (tmp.size() == 4) {
        target.insert(tmp);
      }
    }
    string ans = "";
    int i = 1;
    int alpha;
    int beta;
    while (i) {
      if (i % 13 == 0)
        continue;
      rep(j, 26) {
        rep(k, 2) {
          string sub;
          if (k == 0) {
            sub = "this";
          }
          if (k == 1) {
            sub = "that";
          }
          rep(l, 4) { sub[l] = ((i * (sub[l] - 'a') + j) % MOD) + 'a'; }
          if (target.count((string)sub)) {
            alpha = i;
            beta = j;
            goto end;
          }
        }
      }
      i += 2;
    }
  end:;
    map<char, char> memo;
    rep(i, 26) { memo[((alpha * i + beta) % MOD) + 'a'] = i + 'a'; }
    rep(i, strlen(str)) {
      if (str[i] == ' ')
        ans += " ";
      else
        ans += memo[str[i]];
    }
    cout << ans << endl;
  }
  return 0;
}