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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define INF 1000000000
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;

string A(string s) {
  reverse(s.begin(), s.end());
  return s;
}

string J(string s) {
  string ss = s.substr(s.length() - 1, 1), sss = s.substr(0, s.length() - 1);
  return ss + sss;
}

string C(string s) {
  string ss = s.substr(0, 1), sss = s.substr(1, s.length() - 1);
  return sss + ss;
}

string E(string s) {
  if (s.length() % 2 == 0) {
    string ss = s.substr(0, s.length() / 2);
    string sss = s.substr(s.length() / 2, s.length() / 2);
    return sss + ss;
  } else {
    string ss = s.substr(0, s.length() / 2);
    string tt = s.substr(s.length() / 2, 1);
    string sss = s.substr(s.length() / 2 + 1, s.length() / 2);
    return sss + tt + ss;
  }
}

string P(string s) {
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '9' or s[i] == '1' or s[i] == '2' or s[i] == '3' or
        s[i] == '4' or s[i] == '5' or s[i] == '6' or s[i] == '7' or
        s[i] == '8') {
      s[i] = (char)((int)s[i] - 1);
      continue;
    }
    if (s[i] == '0') {
      s[i] = '9';
    }
  }
  return s;
}

string M(string s) {
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '0' or s[i] == '1' or s[i] == '2' or s[i] == '3' or
        s[i] == '4' or s[i] == '5' or s[i] == '6' or s[i] == '7' or
        s[i] == '8') {
      s[i] = (char)((int)s[i] + 1);
      continue;
    }
    if (s[i] == '9') {
      s[i] = '0';
    }
  }
  return s;
}

int main() {
  int n;
  cin >> n;
  for (int wq = 0; wq < n; wq++) {
    string t, s;
    cin >> t >> s;
    for (int i = t.length() - 1; i >= 0; i--) {
      if (t[i] == 'J')
        s = J(s);
      if (t[i] == 'C')
        s = C(s);
      if (t[i] == 'E')
        s = E(s);
      if (t[i] == 'A')
        s = A(s);
      if (t[i] == 'P')
        s = P(s);
      if (t[i] == 'M')
        s = M(s);
    }
    cout << s << endl;
  }
}

Error: WA

Fixed submission:


#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define INF 1000000000
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;

string A(string s) {
  reverse(s.begin(), s.end());
  return s;
}

string J(string s) {
  string ss = s.substr(s.length() - 1, 1), sss = s.substr(0, s.length() - 1);
  return ss + sss;
}

string C(string s) {
  string ss = s.substr(0, 1), sss = s.substr(1, s.length() - 1);
  return sss + ss;
}

string E(string s) {
  if (s.length() % 2 == 0) {
    string ss = s.substr(0, s.length() / 2);
    string sss = s.substr(s.length() / 2, s.length() / 2);
    return sss + ss;
  } else {
    string ss = s.substr(0, s.length() / 2);
    string tt = s.substr(s.length(), 1);
    string sss = s.substr(s.length() / 2 + 1, s.length() / 2);
    return sss + tt + ss;
  }
}

string P(string s) {
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '9' or s[i] == '1' or s[i] == '2' or s[i] == '3' or
        s[i] == '4' or s[i] == '5' or s[i] == '6' or s[i] == '7' or
        s[i] == '8') {
      s[i] = (char)((int)s[i] - 1);
      continue;
    }
    if (s[i] == '0') {
      s[i] = '9';
    }
  }
  return s;
}

string M(string s) {
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '0' or s[i] == '1' or s[i] == '2' or s[i] == '3' or
        s[i] == '4' or s[i] == '5' or s[i] == '6' or s[i] == '7' or
        s[i] == '8') {
      s[i] = (char)((int)s[i] + 1);
      continue;
    }
    if (s[i] == '9') {
      s[i] = '0';
    }
  }
  return s;
}

int main() {
  int n;
  cin >> n;
  for (int wq = 0; wq < n; wq++) {
    string t, s;
    cin >> t >> s;
    for (int i = t.length() - 1; i >= 0; i--) {
      if (t[i] == 'J')
        s = J(s);
      if (t[i] == 'C')
        s = C(s);
      if (t[i] == 'E')
        s = E(s);
      if (t[i] == 'A')
        s = A(s);
      if (t[i] == 'P')
        s = P(s);
      if (t[i] == 'M')
        s = M(s);
    }
    cout << s << endl;
  }
}