Problem p00040 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define vci vector<int>
#define vcs vector<string>
#define pb push_back
#define sz size()
#define mapii map<int, int>
#define mapci map<char, int>
#define mapsi map<string, int>
#define all(x) x.begin(), x.end()
#define minit(a, i) memset(a, i, sizeof(a));

#define for_(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define for_d(i, a, b) for (int i = (int)a - 1; i >= b; i--)
#define for_r(i, a, b, c) for (int i = (int)a; i < (int)b; i += c)
#define for_dr(i, a, b, c) for (int i = (int)a - 1; i >= b; i -= c)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

typedef long long ll;

const int iINF = 2147483647;
const ll lINF = 9223372036854775807;

template <class T> inline void dbg(T t) { cout << t << endl; }

vcs splitAll(string s, string t) {
  vcs r;
  for (int p = 0; (p = s.find(t)) != s.npos;) {
    r.pb(s.substr(0, p));
    s = s.substr(p + t.size());
  }
  r.pb(s);
  return r;
}

int BETA;
int ALPH;

char func(char c) {
  int t = (ALPH * ((int)(c - 'a')) - BETA + 26) % 26;
  char ret = 'a' + t;
  return ret;
}

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

int main() {
  int n;
  cin >> n;
  cin.ignore(1);

  for_(i, 0, n) {
    string in;
    getline(cin, in);

    int m = (int)in.sz;
    BETA = 0;
    ALPH = 0;
    while (1) {
      if (BETA == 26) {
        BETA = 0;
        while (gcd(++ALPH, 26) != 1) {
        }
      }

      string str = in;
      for_(j, 0, m) {
        if (str[j] == ' ')
          continue;
        str[j] = func(str[j]);
      }
      if (str.find("this") != str.npos || str.find("that") != str.npos) {
        in = str;
        break;
      }
      BETA++;
    }

    cout << in << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define vci vector<int>
#define vcs vector<string>
#define pb push_back
#define sz size()
#define mapii map<int, int>
#define mapci map<char, int>
#define mapsi map<string, int>
#define all(x) x.begin(), x.end()
#define minit(a, i) memset(a, i, sizeof(a));

#define for_(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define for_d(i, a, b) for (int i = (int)a - 1; i >= b; i--)
#define for_r(i, a, b, c) for (int i = (int)a; i < (int)b; i += c)
#define for_dr(i, a, b, c) for (int i = (int)a - 1; i >= b; i -= c)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

typedef long long ll;

const int iINF = 2147483647;
const ll lINF = 9223372036854775807;

template <class T> inline void dbg(T t) { cout << t << endl; }

vcs splitAll(string s, string t) {
  vcs r;
  for (int p = 0; (p = s.find(t)) != s.npos;) {
    r.pb(s.substr(0, p));
    s = s.substr(p + t.size());
  }
  r.pb(s);
  return r;
}

char func(char c) {
  int t = ('c' - 'a' - 2) % 26;
  char ret = 'a' + t;
  return ret;
}

vcs convert(vcs v) {
  vcs ret;
  int m = (int)v.sz;

  for_(i, 0, m) {
    string s = v[i];
    int len = (int)s.sz;
    for_(j, 0, len) s[i] = func(s[i]);
    ret.pb(s);
  }
}

int main() {
  int n;
  cin >> n;

  for_(i, 0, n) {
    string in;
    getline(cin, in);

    vcs v = splitAll(in, " ");
    vcs ret = convert(v);

    int m = (int)ret.sz;
    for_(j, 0, m - 1) cout << ret[j] << " ";
    cout << ret[m - 1] << endl;
  }

  return 0;
}