alexjercan/bugnet huggingface dataset:
Problem p00038 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int a[6] = {};
  while (scanf("%d", &a[0]) != EOF) {
    for (int i = 1; i < 5; i++)
      scanf(",%d", &a[i]);
    int cnt = 0, flag = true, card[13] = {}, max = 4;
    sort(a, a + 5);
    for (int i = 0; i < 4; i++)
      for (int j = i + 1; j < 5; j++)
        if (a[i] == a[j])
          cnt++;
    int i = 0;
    if (a[i] == 1 && a[i + 1] == 10) {
      i = 1;
      max = 5;
      a[5] = 14;
    }
    for (; i < max; i++)
      if (a[i] + 1 != a[i + 1])
        flag = 0;
    if (cnt == 6)
      printf("four card\\n");
    else if (cnt == 4)
      printf("full house\\n");
    else if (flag)
      printf("straight\\n");
    else if (cnt == 3)
      printf("three card\\n");
    else if (cnt == 2)
      printf("two pair\\n");
    else if (cnt == 1)
      printf("one pair\\n");
    else
      printf("null\\n");
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int a[6] = {};
  while (scanf("%d", &a[0])) {
    for (int i = 1; i < 5; i++)
      scanf(",%d", &a[i]);
    int cnt = 0, flag = true, card[13] = {}, max = 4;
    sort(a, a + 5);
    for (int i = 0; i < 4; i++)
      for (int j = i + 1; j < 5; j++)
        if (a[i] == a[j])
          cnt++;
    int i = 0;
    if (a[i] == 1 && a[i + 1] == 10) {
      i = 1;
      max = 5;
      a[5] = 14;
    }
    for (; i < max; i++)
      if (a[i] + 1 != a[i + 1])
        flag = 0;
    if (cnt == 6)
      printf("four card\\n");
    else if (cnt == 4)
      printf("full house\\n");
    else if (flag)
      printf("straight\\n");
    else if (cnt == 3)
      printf("three card\\n");
    else if (cnt == 2)
      printf("two pir\\n");
    else if (cnt == 1)
      printf("one pair\\n");
    else
      printf("null\\n");
  }
}
Problem p00039 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  map<char, int> m;
  m['I'] = 1;
  m['V'] = 5;
  m['X'] = 10;
  m['L'] = 50;
  m['C'] = 100;
  m['D'] = 500;
  m['M'] = 1000;
  string s;
  while (cin >> s) {
    reverse(s.begin(), s.end());
    int p = 0;
    p += m[s[0]];
    for (int i = 1; i < s.size(); i++)
      if (m[s[i]] < m[s[i - 1]])
        p -= m[s[i]];
      else
        p += m[s[i]];
    cout << p << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  while (cin >> s) {
    int b[2][100000] = {0};
    bool bb[2][100000] = {0};
    int a[s.size()], sum = 0;
    for (int i = 0; i < s.size(); i++) {
      int t;
      if (s[i] == 'I')
        t = 1;
      if (s[i] == 'V')
        t = 5;
      if (s[i] == 'X')
        t = 10;
      if (s[i] == 'L')
        t = 50;
      if (s[i] == 'C')
        t = 100;
      if (s[i] == 'D')
        t = 500;
      if (s[i] == 'M')
        t = 1000;
      a[i] = t;
      if (i && a[i] != a[i - 1] && b[0][a[i]])
        b[1][t]++;
      else
        b[0][t]++;
    }
    for (int i = s.size() - 1; i >= 0; i--) {
      if (sum < a[i]) {
        if (i != s.size() - 1 && a[i] != a[i + 1] && bb[0][a[i]]) {
          sum -= b[1][a[i]] * a[i];
          i -= b[1][a[i]] - 1;
        } else {
          sum += b[0][a[i]] * a[i];
          i -= b[0][a[i]] - 1;
          bb[0][a[i]] = 1;
        }
      } else {
        sum -= b[0][a[i]] * a[i];
        i -= b[0][a[i]] - 1;
        bb[0][a[i]] = 1;
      }
    }
    cout << sum << endl;
  }
}
Problem p00039 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

int changeTheChar(char c) {

  if (c == 'I') {
    return 1;
  } else if (c == 'V') {
    return 5;
  } else if (c == 'X') {
    return 10;
  } else if (c == 'L') {
    return 50;
  } else if (c == 'C') {
    return 100;
  } else if (c == 'D') {
    return 500;
  } else if (c == 'M') {
    return 1000;
  }

  return 0;
}

int main() {
  string str;
  while (cin >> str) {

    vector<int> roman(str.size(), 0);
    // ????????????
    for (int i = 0; i < str.size(); i++) {
      roman[i] = changeTheChar(str[i]);
    }

    for (int i = 0; i < roman.size(); i++) {
      // cout << roman[i] << " ";
    }

    int res = 0;
    while (roman.size() > 0) {
      int maxI = 0;
      int maxIndex;
      // ?????§???????´¢
      for (int i = roman.size() - 1; i >= 0; i--) {
        if (maxI <= roman[i]) {
          maxI = roman[i];
          maxIndex = i;
        }
      }
      // cout << maxIndex << endl;
      // cout << maxI << endl;

      if (maxIndex != 0) {
        res += roman[maxIndex] - roman[maxIndex - 1];
        roman.erase(roman.begin());
        roman.erase(roman.begin());
      } else {
        res += roman[maxIndex];
        roman.erase(roman.begin());
      }

      // cout << res << endl;
    }

    cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int changeTheChar(char c) {

  if (c == 'I') {
    return 1;
  } else if (c == 'V') {
    return 5;
  } else if (c == 'X') {
    return 10;
  } else if (c == 'L') {
    return 50;
  } else if (c == 'C') {
    return 100;
  } else if (c == 'D') {
    return 500;
  } else if (c == 'M') {
    return 1000;
  }

  return 0;
}

int main() {

  while (true) {

    string str;
    cin >> str;

    vector<int> roman(str.size(), 0);
    // ????????????
    for (int i = 0; i < str.size(); i++) {
      roman[i] = changeTheChar(str[i]);
    }

    for (int i = 0; i < roman.size(); i++) {
      // cout << roman[i] << " ";
    }

    int res = 0;
    while (roman.size() > 0) {
      int maxI = 0;
      int maxIndex;
      // ?????§???????´¢
      for (int i = roman.size() - 1; i >= 0; i--) {
        if (maxI <= roman[i]) {
          maxI = roman[i];
          maxIndex = i;
        }
      }
      // cout << maxIndex << endl;
      // cout << maxI << endl;

      if (maxIndex != 0) {
        res += roman[maxIndex] - roman[maxIndex - 1];
        roman.erase(roman.begin());
        roman.erase(roman.begin());
      } else {
        res += roman[maxIndex];
        roman.erase(roman.begin());
      }

      // cout << res << endl;
    }

    cout << res << endl;
  }

  return 0;
}
Problem p00039 (C++) — buggy submission, verdict: Runtime Error

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>

typedef long long ll;
using namespace std;

// char name[] = {'I','V','X','L','C','D','M'};
// int num[] = {1,5,10,50,100,500,1000};

int roma2num(char c) {
  return c == 'I'   ? 1
         : c == 'V' ? 5
         : c == 'X' ? 10
         : c == 'L' ? 50
         : c == 'C' ? 100
         : c == 'D' ? 500
                    : 1000;
}

int main() {

  string str;
  while (cin >> str) {
    int sum = 0;
    int i = 0;
    for (; i < str.size() - 1; i++) {
      int now = roma2num(str[i]);
      int next = roma2num(str[i + 1]);

      if (now >= next) {
        sum += now;
      } else {
        sum += next - now;
        i++;
      }
    }

    if (i < str.size())
      sum += roma2num(str[i]);

    cout << sum << endl;
  }
}

Error: -11

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>

typedef long long ll;
using namespace std;

// char name[] = {'I','V','X','L','C','D','M'};
// int num[] = {1,5,10,50,100,500,1000};

int roma2num(char c) {
  return c == 'I'   ? 1
         : c == 'V' ? 5
         : c == 'X' ? 10
         : c == 'L' ? 50
         : c == 'C' ? 100
         : c == 'D' ? 500
                    : 1000;
}

int main() {
  string str;
  while (1) {
    cin >> str;
    int sum = 0;
    int i = 0;
    for (; i < str.size() - 1; i++) {
      int now = roma2num(str[i]);
      int next = roma2num(str[i + 1]);

      if (now >= next) {
        sum += now;
      } else {
        sum += next - now;
        i++;
      }
    }

    if (i < str.size())
      sum += roma2num(str[i]);

    cout << sum << endl;
    str.clear();

    if (cin.eof())
      break;
  }
}
Problem p00039 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;

int main() {
  int sum, i, j, kari;
  string data;
  while (getline(cin, data)) {
    data = data.insert(data.length(), "KKKKKK");
    sum = j = 0;

    for (j = 0; j < data.length(); j++) {
      if (data[j] == 'M') {
        sum += 1000;
      } else if (data[j] == 'D') {
        if (data[j + 1] == 'M') {
          sum += 900;
          j++;
        } else {
          sum += 500;
        }
      } else if (data[j] == 'C') {
        if (data[j + 1] == 'D' || data[j + 1] == 'M') {
          if (data[j + 1] == 'D') {
            sum += 400;
            j++;
          } else {
            sum += 900;
            j++;
          }
        } else {
          sum += 100;
        }
      } else if (data[j] == 'L') {
        sum += 50;
      } else if (data[j] == 'X') {
        if (data[j + 1] == 'L' || data[j + 1] == 'C') {
          if (data[j + 1] == 'L') {
            sum += 40;
            j++;
          } else {
            sum += 90;
            j++;
          }
        } else {
          sum += 10;
        }
      } else if (data[j] == 'V') {
        sum += 5;
      } else if (data[j] == 'I') {
        if (data[j + 1] == 'V' || data[j + 1] == 'X') {
          if (data[j + 1] == 'V') {
            sum += 4;
            j++;
          } else {
            sum += 9;
            j++;
          }
        } else {
          sum += 1;
        }
      }
    }

    cout << sum << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

int main() {
  int sum, i, j, kari;
  string data, str;
  while (getline(cin, str)) {
    data = "KKKKKKKKKKKKKKKKKKKKKKKKKKKKK";
    data = data.replace(0, str.length(), str);
    sum = j = 0;

    while (data[j] != 'K') {
      if (data[j] == 'M') {
        sum += 1000;
      } else if (data[j] == 'D') {
        if (data[j + 1] == 'M') {
          sum += 900;
          j++;
        } else {
          sum += 500;
        }
      } else if (data[j] == 'C') {
        if (data[j + 1] == 'D' || data[j + 1] == 'M') {
          if (data[j + 1] == 'D') {
            sum += 400;
            j++;
          } else {
            sum += 900;
            j++;
          }
        } else {
          sum += 100;
        }
      } else if (data[j] == 'L') {
        sum += 50;
      } else if (data[j] == 'X') {
        if (data[j + 1] == 'L' || data[j + 1] == 'C') {
          if (data[j + 1] == 'L') {
            sum += 40;
            j++;
          } else {
            sum += 90;
            j++;
          }
        } else {
          sum += 10;
        }
      } else if (data[j] == 'V') {
        sum += 5;
      } else if (data[j] == 'I') {
        if (data[j + 1] == 'V' || data[j + 1] == 'X') {
          if (data[j + 1] == 'V') {
            sum += 4;
            j++;
          } else {
            sum += 9;
            j++;
          }
        } else {
          sum += 1;
        }
      }
      j++;
    }

    cout << sum << endl;
  }

  return 0;
}
Problem p00040 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  string str;
  getline(cin, str);
  while (n--) {
    getline(cin, str);
    bool f = false;
    for (int a = 1; !f; a++) {
      if (a % 2 == 0 || a % 13 == 0)
        continue;
      for (int b = 0; !f && b < 26; b++) {
        char m[26];
        for (int i = 0; i < 26; i++)
          m[(i * a + b) % 26] = i + 'a';
        string t = str;
        for (int i = 0; i < t.size(); i++) {
          if (islower(t[i]))
            t[i] = m[t[i] - 'a'];
        }
        bool f_this = (t.find("this") != string::npos);
        bool f_that = (t.find("that") != string::npos);
        if (f_this || f_that) {
          str = t;
          f = true;
        }
      }
    }
    cout << str << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  while (n--) {
    cin.ignore();
    string str;
    getline(cin, str);
    bool f = false;
    for (int a = 1; !f; a++) {
      if (a % 2 == 0 || a % 13 == 0)
        continue;
      for (int b = 0; !f && b < 26; b++) {
        char m[26];
        for (int i = 0; i < 26; i++)
          m[(i * a + b) % 26] = i + 'a';
        string t = str;
        for (int i = 0; i < t.size(); i++) {
          if (islower(t[i]))
            t[i] = m[t[i] - 'a'];
        }
        bool f_this = (t.find("this") != string::npos);
        bool f_that = (t.find("that") != string::npos);
        if (f_this || f_that) {
          str = t;
          f = true;
        }
      }
    }
    cout << str << endl;
  }
}
Problem p00040 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>

using namespace std;

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

int lcm(int x, int y) { return (x * y) / gcd(x, y); }

string affine(string s, int a, int b) {
  string news;
  news.clear();
  for (int i = 0; i < s.length(); i++) {
    if (!('a' <= s[i] && s[i] <= 'z')) {
      news += s[i];
      continue;
    }
    int y = s[i] - 'a';
    news += (char)((a * y + b) % 26 + 'a');
  }

  return news;
}

int main() {

  int N;
  string s;
  cin >> N;
  cin.ignore();
  while (N-- > 0) {
    getline(cin, s, '\\n');
    bool fin = false;
    for (int a = 0; a <= 50; a++) {
      if (gcd(a, 26) == 1) {
        for (int b = 0; b < 26; b++) {
          string Ns = affine(s, a, b);
          if (Ns.find("that") != string::npos ||
              Ns.find("this") != string::npos) {
            cout << Ns << endl;
            fin = true;
            break;
          }
        }
      }
      if (fin)
        break;
    }
    if (!fin)
      cout << s << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

int lcm(int x, int y) { return (x * y) / gcd(x, y); }

string affine(string s, int a, int b) {
  string news;
  for (int i = 0; i < s.length(); i++) {
    if (!('a' <= s[i] && s[i] <= 'z')) {
      news += s[i];
      continue;
    }
    int y = s[i] - 'a';
    news += (char)((a * y + b) % 26 + 'a');
  }

  return news;
}

int main() {

  int N;
  string s;
  cin >> N;
  cin.ignore();
  while (N-- > 0) {
    getline(cin, s, '\\n');

    bool fin = false;
    for (int a = 0; a <= 1000; a++) {
      if (gcd(a, 26) == 1) {
        for (int b = 0; b <= 1000; b++) {
          string Ns = affine(s, a, b);
          if (Ns.find("that") != string::npos ||
              Ns.find("This") != string::npos) {
            cout << Ns << endl;
            fin = true;
            break;
          }
        }
      }
      if (fin)
        break;
    }
    if (!fin)
      cout << s << endl;
  }
  return 0;
}
Problem p00040 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<int> a;
vector<int> b;
int id = 2;
int N;
int yukuri(int n, int m) {
  if (n < m)
    swap(n, m);
  while (m > 0) {
    int tmp = n % m;
    n = m;
    m = tmp;
  }
  return n;
}

void add_ab() {
  ++id;
  while (yukuri(id, 26) != 1)
    id++;
  for (int i = 0; i < 26; i++) {
    a.push_back(id);
    b.push_back(i);
  }
}
int F(char y, int id) { return ((int)(y - 'a') * a[id] + b[id]) % 26; }
string change(string s, int id) {
  for (int i = 0; i < (int)s.size() - 1; i++) {
    if (s[i] == ' ')
      continue;
    s[i] = (char)F(s[i], id) + 'a';
  }
  return s;
}

int main() {
  scanf("%d", &N);
  add_ab();

  getchar();
  for (int i = 0; i < N; i++) {
    char buf[258];
    fgets(buf, sizeof(buf), stdin);
    string s = buf;
    for (int i = 0; i < (int)a.size(); i++) {
      s = change(s, i);
      if (0 <= (int)s.find("that") || 0 <= (int)s.find("this")) {
        printf("%s", s.c_str());
        break;
      }
      //      printf("ng:: %s",s.c_str());
      s = buf;
      if (i == (int)a.size() - 1) {
        add_ab();
      }
    }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<int> a;
vector<int> b;
int id = 2;
int N;
int yukuri(int n, int m) {
  if (n < m)
    swap(n, m);
  while (m > 0) {
    int tmp = n % m;
    n = m;
    m = tmp;
  }
  return n;
}

void add_ab() {
  ++id;
  while (yukuri(id, 26) != 1)
    id++;
  for (int i = 0; i < 26; i++) {
    a.push_back(id);
    b.push_back(i);
  }
}
int F(char y, int id) { return ((int)(y - 'a') * a[id] + b[id]) % 26; }
string change(string s, int id) {
  for (int i = 0; i < (int)s.size() - 1; i++) {
    if (s[i] == ' ')
      continue;
    s[i] = (char)F(s[i], id) + 'a';
  }
  return s;
}

int main() {
  scanf("%d", &N);
  add_ab();
  for (int i = 0; i < N; i++) {
    char buf[258];
    getchar();
    fgets(buf, sizeof(buf), stdin);
    string s = buf;
    for (int i = 0; i < (int)a.size(); i++) {
      s = change(s, i);
      if (0 <= (int)s.find("that") || 0 <= (int)s.find("this")) {
        printf("%s", s.c_str());
        break;
      }
      //      printf("ng:: %s",s.c_str());
      s = buf;
      if (i == (int)a.size() - 1) {
        add_ab();
      }
    }
  }
}
Problem p00040 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main(void) {
  int n;
  cin >> n;
  getchar();
  for (int i = 0; i < n; i++) {
    string s, ss;
    getline(cin, s);
    bool f = true;
    for (int a = 0; f; a++) {
      for (int b = 0; b < 26; b++) {
        ss = s;
        for (int j = 0; j < (int)s.size(); j++) {
          if (s[j] != ' ') {
            ss[j] = ((s[j] - 'a') * a + b) % 26 + 'a';
          }
        }
        if ((int)ss.find("this") != -1 || (int)ss.find("that") != -1) {
          f = false;
          break;
        }
      }
    }
    cout << ss << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main(void) {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s, ss;
    getchar();
    getline(cin, s);
    bool f = true;
    for (int a = 0; f; a++) {
      for (int b = 0; b < 26; b++) {
        ss = s;
        for (int j = 0; j < (int)s.size(); j++) {
          if (s[j] != ' ') {
            ss[j] = ((s[j] - 'a') * a + b) % 26 + 'a';
          }
        }
        if ((int)ss.find("this") != -1 || (int)ss.find("that") != -1) {
          f = false;
          break;
        }
      }
    }
    cout << ss << endl;
  }
}
Problem p00040 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
string change(int a, int b, string str) {
  for (int i = 0; i < str.size(); i++)
    str[i] = ((str[i] - 'a') * a + b) % 26 + 'a';
  return str;
}
string out(int a, int b, string str) {
  for (int i = 0; i < str.size(); i++) {
    if ('a' <= str[i] && str[i] <= 'z') {
      for (int j = 0; j < 26; j++) {
        if ((j * a + b) % 26 + 'a' == str[i]) {
          str[i] = j + 'a';
          break;
        }
      }
    }
  }
  return str;
}
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    string str;
    if (!i)
      getchar();
    getline(cin, str);
    for (int j = 1;; j++) {
      for (int k = 0; k < 26; k++) {
        for (int l = 0; l < str.size() - 3; l++) {
          if (change(j, k, "that") == str.substr(l, 4)) {
            cout << out(j, k, str) << endl;
            goto exit;
          }
          if (change(j, k, "this") == str.substr(l, 4)) {
            cout << out(j, k, str) << endl;
            goto exit;
          }
        }
      }
    }
  exit:;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
string change(int a, int b, string str) {
  for (int i = 0; i < str.size(); i++)
    str[i] = ((str[i] - 'a') * a + b) % 26 + 'a';
  return str;
}
string out(int a, int b, string str) {
  for (int i = 0; i < str.size(); i++) {
    if ('a' <= str[i] && str[i] <= 'z') {
      for (int j = 0; j < 26; j++) {
        if ((j * a + b) % 26 + 'a' == str[i]) {
          str[i] = j + 'a';
          break;
        }
      }
    }
  }
  return str;
}
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    string str;
    getchar();
    getline(cin, str);
    for (int j = 1;; j++) {
      for (int k = 0; k < 26; k++) {
        for (int l = 0; l < str.size() - 3; l++) {
          if (change(j, k, "that") == str.substr(l, 4)) {
            cout << out(j, k, str) << endl;
            goto exit;
          }
          if (change(j, k, "this") == str.substr(l, 4)) {
            cout << out(j, k, str) << endl;
            goto exit;
          }
        }
      }
    }
  exit:;
  }
  return 0;
}
Problem p00040 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  string abc = "abcdefghijklmnopqrstuvwxyz";
  string n;
  int nn;
  char c;
  cin >> nn >> c;
  for (int p = 0; p < nn; p++) {
    getline(cin, n);
    if (p == 0) {
      n = c + n;
    }
    vector<int> data(n.size(), -1);
    for (int i = 0; i < n.size(); i++) {
      for (int j = 0; j < abc.size(); j++) {
        if (n[i] == abc[j]) {
          data[i] = j;
          break;
        }
      }
    }
    string b;
    for (int i = 3;; i++) {
      if (i % 2 != 0 && 1 % 13 != 0) {
        bool hantei = false;
        for (int j = 0; j < 26; j++) {
          string c = "";
          for (int k = 0; k < n.size(); k++) {
            if (data[k] == -1) {
              c += ' ';
            } else {
              c += abc[(i * data[k] + j) % 26];
            }
          }
          if (c.find("this") != string::npos ||
              c.find("that") != string::npos) {
            b = c;
            hantei = true;
            break;
          }
        }
        if (hantei) {
          break;
        }
      }
    }
    cout << b << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

int f(int a, int b, int y) { return (a * y + b) % 26; }

int main() {
  int n;
  string abc = "abcdefghijklmnopqrstuvwxyz";
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    getline(cin, s);
    int a, b;
    a = b = 0;
    while (true) {
      string::size_type index = s.find("this");
      string::size_type index2 = s.find("that");
      if (index != string::npos || index2 != string::npos) {
        break;
      }
      for (int i = 0; i < s.size(); i++) {
        for (int j = 0; j < 26; j++) {
          if (s[i] == abc[j]) {
            s[i] = f(a, b, j);
          }
        }
      }
      a++;
      b++;
    }
    cout << s << endl;
  }
  return 0;
}