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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) (a).begin(), (a).end()
#define pb(x) push_back(x)
#define ms(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge {
  int from, to, cost;
};

string s, mes;
string solve(string s, char c) {
  if (c == 'C')
    return s.substr(1) + s[0];
  if (c == 'J')
    return s[s.size() - 1] + s.substr(0, s.size() - 1);
  if (c == 'E') {
    if (s.size() % 2 == 0)
      return s.substr(s.size() / 2, s.size() - 1) + s.substr(0, s.size() / 2);
    else
      return s.substr(s.size() / 2 + 1, s.size()) + s[s.size() / 2] +
             s.substr(0, s.size() / 2);
  }
  if (c == 'A') {
    string r = "";
    REP(i, s.size()) r += s[s.size() - i - 1];
    return r;
  }
  if (c == 'M') {
    string r = "";
    REP(i, s.size()) {
      if (isdigit(s[i])) {
        if (s[i] == '9')
          r += '0';
        else
          r += s[i] + 1;
      } else
        r += s[i];
    }
    return r;
  }
  if (c == 'P') {
    string r = "";
    REP(i, s.size()) {
      if (isdigit(s[i])) {
        if (s[i] == '0')
          r += '9';
        else
          r += s[i] - 1;
      } else {
        r += s[i];
      }
    }
    return r;
  }
}
int main() {
  int n;
  cin >> n;

  REP(i, n) {
    cin >> mes >> s;
    for (int i = mes.size() - 1; i >= 0; i--) {
      s = solve(s, mes[i]);
    }
    cout << s << endl;
  }
}

Error: -6
free(): double free detected in tcache 2


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) (a).begin(), (a).end()
#define pb(x) push_back(x)
#define ms(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge {
  int from, to, cost;
};

string s, mes;
string solve(string s, char c) {
  if (c == 'J')
    return s.substr(1) + s[0];
  if (c == 'C')
    return s[s.size() - 1] + s.substr(0, s.size() - 2);
  if (c == 'E') {
    if (s.size() % 2 == 0)
      return s.substr(s.size() / 2, s.size() - 1) + s.substr(0, s.size() / 2);
    else
      return s.substr(s.size() / 2 + 1, s.size()) + s[s.size() / 2] +
             s.substr(0, s.size() / 2);
  }
  if (c == 'A') {
    string r = "";
    REP(i, s.size()) r += s[s.size() - i - 1];
    return r;
  }
  if (c == 'P') {
    string r = "";
    REP(i, s.size()) {
      if (isdigit(s[i])) {
        if (s[i] == '9')
          r += '0';
        else
          r += s[i] + 1;
      } else
        r += s[i];
    }
  }
  if (c == 'M') {
    string r = "";
    REP(i, s.size()) {
      if (isdigit(s[i])) {
        if (s[i] == '0')
          r += '9';
        else
          r += s[i] - 1;
      } else {
        r += s[i];
      }
    }
  }
}
int main() {
  int n;
  cin >> n;

  REP(i, n) {
    cin >> mes >> s;
    REP(i, mes.size()) { s = solve(s, mes[i]); }
    cout << s << endl;
  }
}