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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
using namespace std;
struct NODE {
  int lid, llen;
  int rid, rlen;
  string rem;
  NODE() {}
  NODE(int _l, int _ll, int _r, int _rl, string &s) {
    lid = _l;
    llen = _ll;
    rid = _r;
    rlen = _rl;
    rem = s;
  }
};
bool operator<(const NODE &a, const NODE &b) {
  if (a.lid != b.lid)
    return a.lid < b.lid;
  if (a.rid != b.rid)
    return a.rid < b.rid;
  if (a.llen != b.llen)
    return a.llen < b.llen;
  if (a.rlen != b.rlen)
    return a.rlen < b.rlen;
  return a.rem < b.rem;
}
map<NODE, int> hash;
map<NODE, int> fin;
int conn[101][101];
int n;
string str[101], rev[101];
bool judge(string &s, string &t) {
  int r = min(t.size(), s.size());
  for (int i = 0, ps = 0, pt = t.size() - 1; i < r; i++, ps++, pt--)
    if (s[ps] != t[pt])
      return false;
  return true;
}
bool end(NODE &s) {
  if (conn[s.lid][s.rid] == false)
    return false;
  for (int i = 0; i < s.rem.size() / 2; i++)
    if (s.rem[i] != s.rem[s.rem.size() - i - 1])
      return false;
  return true;
}
int dfs(NODE s, int dep) {
  fin[s]++;
  // for(int i= 0; i< dep*2;i++)
  //     putchar('-');
  // printf("l = %d ll = %d r= %d rl = %d
  // %s\\n",s.lid,s.llen,s.rid,s.rlen,s.rem.c_str());
  if (hash.count(s) > 0 && fin[s] == 2) {
    if (hash[s] >= 0) {
      if (fin[s] != 0)
        return -2;
      return hash[s];
    } else {
      if (end(s))
        return hash[s] = -2;
      return hash[s];
    }
  }
  hash[s] = -1;
  int ret = -1;
  if (end(s)) {
    hash[s] = ret = s.rem.size();
    //    puts("ok");
  }

  if (s.llen == 0) {
    for (int i = 0; i < n; i++) {
      if (conn[s.lid][i] && judge(str[i], s.rem)) {
        if (str[i].size() < s.rem.size()) {
          int nlen = s.rem.size() - str[i].size();
          string nsub = s.rem.substr(0, nlen);
          NODE ns = NODE(i, 0, s.rid, nlen, nsub);
          int ntmp = dfs(ns, dep + 1); // + str[i].size();
          if (ntmp == -2) {
            fin[s]--;
            return hash[s] = -2;
          } else if (ntmp == -1)
            continue;
          else {
            ret = max(ret, ntmp + (int)str[i].size() * 2);
          }
        } else {
          int nlen = str[i].size() - s.rem.size();
          string nsub = str[i].substr(s.rem.size());
          NODE ns = NODE(i, nlen, s.rid, 0, nsub);
          int ntmp = dfs(ns, dep + 1);
          if (ntmp == -2) {
            fin[s]--;
            return hash[s] = -2;
          } else if (ntmp == -1)
            continue;
          else
            ret = max(ret, ntmp + (int)s.rem.size() * 2);
        }
      }
    }
  } else {
    for (int i = 0; i < n; i++) {
      if (conn[i][s.rid] && judge(s.rem, rev[i])) {
        // printf("jiong");
        if (rev[i].size() < s.rem.size()) {
          int nlen = s.rem.size() - rev[i].size();
          string nsub = s.rem.substr(rev[i].size());
          NODE ns = NODE(s.lid, nlen, i, 0, nsub);
          int ntmp = dfs(ns, dep + 1); // + str[i].size();
          if (ntmp == -2) {
            fin[s]--;
            return hash[s] = -2;
          } else if (ntmp == -1)
            continue;
          else
            ret = max(ret, ntmp + (int)rev[i].size() * 2);
        } else {
          int nlen = rev[i].size() - s.rem.size();
          string nsub = rev[i].substr(0, nlen);
          NODE ns = NODE(s.lid, 0, i, nlen, nsub);
          int ntmp = dfs(ns, dep + 1);
          if (ntmp == -2) {
            return hash[s] = -2;
          } else if (ntmp == -1)
            continue;
          else
            ret = max(ret, ntmp + (int)s.rem.size() * 2);
        }
      }
    }
  }
  // for(int i= 0; i< dep*2;i++)
  //     putchar('-');
  // printf("l = %d ll = %d r= %d rl = %d %s
  // %d\\n",s.lid,s.llen,s.rid,s.rlen,s.rem.c_str(),ret);
  fin[s]--;
  return hash[s] = ret;
}
char ts[1000];
int main() {
  // string sa = "ab" ,sb = "ba";
  // printf("%d\\n",judge(sa,sb));
  int m;
  while (scanf("%d%d", &n, &m) == 2) {
    memset(conn, 0, sizeof(conn));
    fin.clear();
    hash.clear();
    for (int i = 0; i < n; i++) {
      scanf("%s", ts);
      str[i] = ts;
      // reverse(ts,ts+strlen(ts));
      rev[i] = ts;
    }
    str[n] = "!";
    rev[n] = "!";
    for (int i = 0; i < n; i++)
      conn[i][n] = conn[n][i] = 1;
    for (int i = 0; i < m; i++) {
      int a, b;
      scanf("%d%d", &a, &b);
      --a;
      --b;
      conn[a][b] = 1;
    }
    string em = "";
    NODE start = NODE(n, 0, n, 0, em);
    int ret = dfs(start, 0);
    if (ret == -2)
      puts("-1");
    else if (ret == -1)
      puts("0");
    else
      printf("%d\\n", ret);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn = 101;

bool mp[maxn][maxn];
char s[maxn][20], res[maxn][20];
int n, len[maxn];
int ans;
bool ansflag;

struct node {
  int idx, lenx, idy, leny, len;
  char now[11];
  node(){};
  node(int a, int b, int c, int d, char *ne, int e) {
    idx = a;
    lenx = b;
    idy = c;
    leny = d;
    strcpy(now, ne);
    len = e;
  }
  bool operator<(const node &other) const {
    if (idx != other.idx)
      return idx < other.idx;
    if (lenx != other.lenx)
      return lenx < other.lenx;
    if (idy != other.idy)
      return idy < other.idy;
    if (leny != other.leny)
      return leny < other.leny;
    return strcmp(now, other.now) < 0;
  }
} P;
map<node, char> hash;

int end(node &P) {
  if (P.lenx == P.leny)
    return 2 * P.len * mp[P.idx][P.idy];
  int t = P.lenx + P.leny;
  for (int i = 0; i < t / 2; i++)
    if (P.now[i] != P.now[t - i - 1])
      return 0;
  return (2 * P.len + P.lenx + P.leny) * mp[P.idx][P.idy];
}

void dfs(int idx, int lenx, int idy, int leny, char *now, int lens) {
  // printf("%d %d %d %d |%s| %d\\n",idx,lenx,idy,leny,now,lens);
  if (!ansflag)
    return;
  P = node(idx, lenx, idy, leny, now, lens);
  // it=hash.find(P);
  if (hash.find(P) == hash.end()) {
    hash[P] = 1;
    ans = max(ans, end(P));
  } else {
    hash[P]++;
    if (hash[P] > 100 && end(P)) {
      // puts("end OK");
      ansflag = 0;
    }
    if (hash[P] > 300)
      return;
  }
  char ne[11];
  if (lenx <= leny) {
    for (int i = 1; i <= n; i++)
      if (mp[idx][i]) {
        int t = min(len[i], leny);

        bool flag = 1;
        for (int j = 0; j < t && flag; j++)
          if (s[i][j] != now[j])
            flag = 0;
        if (flag) {
          if (len[i] < leny)
            for (int j = 0; j < leny - len[i]; j++)
              ne[j] = now[j + len[i]];
          else
            for (int j = 0; j < len[i] - leny; j++)
              ne[j] = s[i][j + leny];
          ne[max(len[i] - t, leny - t)] = 0;
          // puts("K1");
          dfs(i, len[i] - t, idy, leny - t, ne, lens + t);
        }
      }
  } else {
    for (int i = 1; i <= n; i++)
      if (mp[i][idy]) {
        int t = min(len[i], lenx);

        bool flag = 1;
        for (int j = 0; j < t && flag; j++)
          if (res[i][j] != now[j])
            flag = 0;
        if (flag) {
          if (len[i] < lenx)
            for (int j = 0; j < lenx - len[i]; j++)
              ne[j] = now[j + len[i]];
          else
            for (int j = 0; j < len[i] - lenx; j++)
              ne[j] = res[i][j + lenx];
          ne[max(len[i] - t, lenx - t)] = 0;
          // puts("K2");
          dfs(idx, lenx - t, i, len[i] - t, ne, lens + t);
        }
      }
  }
}

int main() {
  int m;

  // freopen("ans.out","w",stdout);
  while (scanf("%d %d", &n, &m) == 2) {
    for (int i = 1; i <= n; i++) {
      scanf("%s", res[i]);
      strcpy(s[i], res[i]);
      len[i] = strlen(res[i]);
      reverse(res[i], res[i] + len[i]);
    }
    memset(mp, 0, sizeof(mp));
    int u, v;
    for (int i = 0; i < m; i++) {
      scanf("%d %d", &u, &v);
      mp[u][v] = 1;
    }
    for (int i = 1; i <= n; i++)
      mp[0][i] = mp[i][0] = 1;
    char now[20] = "";
    hash.clear();
    ans = 0;
    ansflag = 1;
    dfs(0, 0, 0, 0, now, 0);
    if (ansflag)
      printf("%d\\n", ans);
    else
      puts("-1");
  }
  return 0;
}
/*
2 2
a
b
1 2
2 1

2 2
ab
ba
1 2
2 1


*/