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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;
using ll = long long;

string C(const string &S) {
  const int N = S.size();
  string s;
  s.resize(N);
  for (int i = 0; i < N; i++) {
    s[i] = S[(i + 1) % N];
  }
  return s;
}

string J(const string &S) {
  const int N = S.size();
  string s;
  s.resize(N);
  for (int i = 0; i < N; i++) {
    s[i] = S[(i + N - 1) % N];
  }
  return s;
}

string E(const string &S) {
  const int N = S.size();
  const int shift = (N + 1) / 2;
  string s = S;
  for (int i = 0; i < N; i++) {
    if (i + shift < N) {
      s[i] = S[i + shift];
    } else if (i - shift >= 0) {
      s[i] = S[i - shift];
    }
  }
  return s;
}

string A(const string &S) {
  string s = S;
  reverse(s.begin(), s.end());
  return s;
}

string P(const string &S) {
  const int N = S.size();
  string s = S;
  for (int i = 0; i < N; i++) {
    if (s[i] >= '1' and s[i] <= '9') {
      s[i]--;
    } else if (s[i] == '0') {
      s[i] = '9';
    }
  }
  return s;
}

string M(const string &S) {
  const int N = S.size();
  string s = S;
  for (int i = 0; i < N; i++) {
    if (s[i] >= '0' and s[i] <= '8') {
      s[i]++;
    } else if (s[i] == '9') {
      s[i] = '0';
    }
  }
  return s;
}

string op(const char c, const string &S) {
  switch (c) {
  case 'J':
    return J(S);
  case 'C':
    return C(S);
  case 'E':
    return E(S);
  case 'A':
    return A(S);
  case 'P':
    return P(S);
  case 'M':
    return M(S);
  }
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    reverse(s.begin(), s.end());
    string m;
    cin >> m;
    for (const char c : s) {
      m = op(c, m);
    }
    cout << m << endl;
  }
  return 0;
}

Error: -6
free(): invalid pointer


Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;
using ll = long long;

string J(const string &S) {
  const int N = S.size();
  string s;
  s.resize(N);
  for (int i = 0; i < N; i++) {
    s[i] = S[(i + 1) % N];
  }
  return s;
}

string C(const string &S) {
  const int N = S.size();
  string s;
  s.resize(N);
  for (int i = 0; i < N; i++) {
    s[i] = S[(i + N - 1) % N];
  }
  return s;
}

string E(const string &S) {
  const int N = S.size();
  const int shift = (N + 1) / 2;
  string s;
  s.resize(N);
  for (int i = 0; i < N; i++) {
    if (i + shift < N) {
      s[i] = S[i + shift];
    } else {
      s[i] = S[i - shift];
    }
  }
  return s;
}

string A(const string &S) {
  string s = S;
  reverse(s.begin(), s.end());
  return s;
}

string P(const string &S) {
  const int N = S.size();
  string s = S;
  for (int i = 0; i < N; i++) {
    if (s[i] >= '1' and s[i] <= '9') {
      s[i]--;
    } else if (s[i] == '0') {
      s[i] = '9';
    }
  }
  return s;
}

string M(const string &S) {
  const int N = S.size();
  string s = S;
  for (int i = 0; i < N; i++) {
    if (s[i] >= '0' and s[i] <= '8') {
      s[i]++;
    } else if (s[i] == '9') {
      s[i] = '0';
    }
  }
  return s;
}

string op(const char c, const string &S) {
  switch (c) {
  case 'J':
    return J(S);
  case 'C':
    return C(S);
  case 'E':
    return E(S);
  case 'A':
    return A(S);
  case 'P':
    return P(S);
  case 'M':
    return M(S);
  }
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    reverse(s.begin(), s.end());
    string m;
    cin >> m;
    for (const char c : m) {
      s = op(c, s);
    }
    cout << s << endl;
  }
  return 0;
}