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

#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

string check(string str);
map<char, int> create_table();
int gcd(int a, int b);

int main() {
  char ch = 'a';
  string str;
  int num;
  vector<string> answers;
  map<char, int> table = create_table();
  cin >> num;

  cin.ignore();
  for (int k = 0; k < num; k++) {
    getline(cin, str);

    char c;
    char list[1];
    bool flag = true;
    int beta;
    int alpha = 1;
    map<char, int>::iterator itr;
    while (flag) {
      if (gcd(alpha, 26) == 1) {
        for (beta = 1; beta <= 26; beta++) {
          string result;
          for (int i = 0; i < str.size(); i++) {
            c = str[i];
            if (c != ' ') {
              sprintf(list, "%c", ch + (alpha * table[c] + beta) % 26);
              result += list[0];
            } else {
              result += ' ';
            }
          }
          if (result.find("this", 0) != string::npos ||
              result.find("that", 0) != string::npos) {
            answers.push_back(result);
            flag = false;
            break;
          }
        }
      }
      alpha++;
    }
  }

  for (int j = 0; j < answers.size(); j++) {
    cout << answers[j] << endl;
  }
  return 0;
}

map<char, int> create_table() {
  map<char, int> table;
  char ch[1];
  char str = 'a';

  for (int i = 0; i < 26; i++) {
    sprintf(ch, "%c", str + i);
    table.insert(pair<char, int>(ch[0], i));
  }
  return table;
}

int gcd(int a, int b) {
  int temp;
  if (a < b) {
    temp = a;
    a = b;
    b = temp;
  }
  while (b != 0) {
    temp = b;
    b = a % b;
    a = temp;
  }
  return a;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

string check(string str);
map<char, int> create_table();
int gcd(int a, int b);

int main() {
  char ch = 'a';
  string str;
  int num;
  vector<string> answers;
  map<char, int> table = create_table();
  cin >> num;

  cin.ignore();
  for (int k = 0; k < num; k++) {
    getline(cin, str);

    char c;
    char list[1];
    bool flag = true;
    int beta;
    int alpha = 1;
    map<char, int>::iterator itr;
    while (flag) {
      if (gcd(alpha, 26) == 1) {
        for (beta = 1; beta < 26; beta++) {
          string result;
          for (int i = 0; i < str.size(); i++) {
            c = str[i];
            if (c != ' ') {
              sprintf(list, "%c", ch + (alpha * table[c] + beta) % 26);
              result += list[0];
            } else {
              result += ' ';
            }
          }
          if (result.find("this", 0) != string::npos ||
              result.find("that", 0) != string::npos) {
            answers.push_back(result);
            flag = false;
            break;
          }
        }
      }
      alpha++;
    }
  }

  for (int j = 0; j < answers.size(); j++) {
    cout << answers[j] << endl;
  }
  return 0;
}

map<char, int> create_table() {
  map<char, int> table;
  char ch[1];
  char str = 'a';

  for (int i = 0; i < 26; i++) {
    sprintf(ch, "%c", str + i);
    table.insert(pair<char, int>(ch[0], i));
  }
  return table;
}

int gcd(int a, int b) {
  int temp;
  while (b == 0) {
    temp = a;
    a = temp / b;
    b = temp % b;
  }
  if (a == 0)
    a = 1;
  return a;
}