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

// suhan lee,saitama university

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define repn(i, n) for (int i = 0; i < n; i++)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
#define pb push_back
#define pf push_front

string rev(string s) {
  string ret = s;
  repn(i, s.size() / 2) { swap(ret[i], ret[s.size() - i - 1]); }
  return ret;
}

int solve() {
  string s;
  cin >> s;
  set<string> ss;
  int n = s.size();
  rep(i, 1, n) {
    string sl = s.substr(0, i);
    string sr = s.substr(i);
    // cout << sl << ", " << sr << endl;
    ss.insert(sr + sl);
    ss.insert(rev(sr) + sl);
    ss.insert(sr + rev(sl));
    ss.insert(rev(sr) + rev(sl));
    ss.insert(sl + sr);
    ss.insert(rev(sl) + sr);
    ss.insert(sl + rev(sr));
    ss.insert(rev(sl) + rev(sr));
  }
  return ss.size();
}

int main() {

  int m;
  cin >> m;
  repn(i, m) cout << solve() << endl;

  return 0;
}

Error: -6
free(): invalid pointer


Fixed submission:

#include <iostream>
#include <string>
using namespace std;
class kamotsu {
public:
  kamotsu *alpha[30];
  bool check;
  kamotsu() {
    for (int i = 0; i < 26; i++)
      alpha[i] = 0;
    check = true;
  }
  ~kamotsu() {
    for (int i = 1; i < 26; i++)
      delete alpha[i];
  }
};
string reverse(string inst) {
  string ret;
  for (int i = 0; i < inst.length(); i++) {
    ret += inst[inst.length() - i - 1];
  }
  return ret;
}

class ressha {
public:
  int end;
  kamotsu *res;
  ressha() {
    end = 0;
    res = new kamotsu[1];
  }
  ~ressha() { delete res; }
  int out() { return end; }

  void inpri(const string newre) {
    kamotsu *tmp;
    tmp = res;
    int i;

    for (i = 0; i < newre.length(); i++) {
      // cout <<"(int)(newre[i]-'a'):"<<(int)(newre[i]-'a')<<endl;
      if (tmp->alpha[(int)(newre[i] - 'a')] == NULL) {
        tmp->alpha[(int)(newre[i] - 'a')] = new kamotsu[1];
      }
      tmp = tmp->alpha[(int)(newre[i] - 'a')];
    }

    if (tmp->check) {
      end++;
      tmp->check = false;
    }
  }

  void in(const string newret) {
    string reta, retb, retar, retbr;
    int ends = newret.length();

    for (int i = 0; i < ends; i++) {

      reta = newret.substr(0, i);
      retb = newret.substr(i, ends - i);
      retar = reverse(reta);
      retbr = reverse(retb);
      inpri(reta + retb);
      inpri(retb + reta);
      inpri(retar + retbr);
      inpri(retbr + retar);
      inpri(reta + retbr);
      inpri(retar + retb);
      inpri(retb + retar);
      inpri(retbr + reta);
    }
  }
};

int main() {
  ressha *start;
  string s;
  int set;

  cin >> set;
  for (int i = 0; i < set; i++) {
    start = new ressha();
    cin >> s;

    (*start).in(s);
    cout << (*start).out() << endl;

    delete start;
  }
  return 0;
}