Problem p01190 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int tbl[6][6] = {
    {-1, 1, 1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1},
    {0, 0, -1, -1, -1, 0},  {2, -1, -1, -1, -1, 2},   {-1, 1, 1, -1, -1, -1},
};

int cnt[500][500][6];
int dat[500][500][6];
int lft[500][500][6];
int rgt[500][500][6];
vector<string> name;

void tostr(int i, int j, int t, bool space, string &res) {
  if (cnt[i][j][t] != 1) {
    throw 0;
  }

  int k = dat[i][j][t];
  int x = lft[i][j][t];
  int y = rgt[i][j][t];

  if (t == 0) {
    tostr(i, k, x, false, res);
    if (space) {
      res += ' ';
    }
    res += '(';
    tostr(k, j, y, false, res);
    res += ')';
  } else if (t == 1 || t == 2) {
    tostr(i, k, x, space, res);
    tostr(k, j, y, true, res);
  } else if (t == 5) {
    if (space) {
      res += ' ';
    }
    res += name[k];
  }
}

string solve(const string &s) {
  memset(cnt, 0, sizeof cnt);
  name.clear();

  int n = 0;
  char buf[32] = {};
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == 'l') {
      cnt[n][n + 1][3] = 1;
      ++n;
    } else if (s[i] == 'n') {
      cnt[n][n + 1][4] = 1;
      ++n;
    } else if (isupper(s[i])) {
      sscanf(s.c_str() + i, "%[A-Z]", buf);
      cnt[n][n + 1][5] = 1;
      dat[n][n + 1][5] = name.size();
      name.push_back(buf);
      i += name.back().size() - 1;
      ++n;
    }
  }

  for (int len = 2; len <= n; ++len) {
    for (int i = 0; i + len <= n; ++i) {
      int j = i + len;
      for (int k = i; k < i + len; ++k) {
        for (int x = 0; x < 6; ++x) {
          if (cnt[i][k][x] == 0) {
            continue;
          }
          for (int y = 0; y < 6; ++y) {
            int t = tbl[x][y];
            if (cnt[k][j][y] == 0 || t == -1) {
              continue;
            }
            if (++cnt[i][j][t] == 1) {
              dat[i][j][t] = k;
              lft[i][j][t] = x;
              rgt[i][j][t] = y;
            }
          }
        }
      }
    }
  }

  if (cnt[0][n][0] == 1) {
    try {
      string ans;
      tostr(0, n, 0, false, ans);
      return ans;
    } catch (...) {
    }
  }

  return "AMBIGUOUS";
}

int main() {
  name.reserve(500);

  int n;
  cin >> n;
  cin.get();
  string s;
  for (int i = 0; i < n; ++i) {
    getline(cin, s);
    cout << solve(s) << '\\n';
  }
}

Error: TLE

Fixed submission:

/*

[S] -> [L][U]
[S] -> [L][S]
[S] -> [L][M]
[M] -> [U][M]
[M] -> [S][M]
[M] -> [U][E]
[M] -> [S][E]
[E] -> [A][U]
[E] -> [A][S]

[U] -> upper
[L] -> l
[A] -> n

----------------------
0: [S]
1: [M]
2: [E]
3: [L]
4: [A]
5: [U]


[0] -> [3][5]
[0] -> [3][0]
[0] -> [3][1]
[1] -> [5][1]
[1] -> [0][1]
[1] -> [5][2]
[1] -> [0][2]
[2] -> [4][5]
[2] -> [4][0]

[5] -> upper
[3] -> l
[4] -> n

*/

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int tbl[6][6] = {
    {-1, 1, 1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1},
    {0, 0, -1, -1, -1, 0},  {2, -1, -1, -1, -1, 2},   {-1, 1, 1, -1, -1, -1},
};

int cnt[500][500][6];
int dat[500][500][6];
int lft[500][500][6];
int rgt[500][500][6];

bool unnec(char c) { return c != 'l' && c != 'n' && !isupper(c); }

string tostr(int i, int j, int t, bool space) {
  if (cnt[i][j][t] != 1) {
    throw 0;
  }

  deque<char> dq;
  int k = dat[i][j][t];
  int x = lft[i][j][t];
  int y = rgt[i][j][t];

  string ret;

  if (t == 0) {
    tostr(i, k, x, false);
    if (space) {
      ret = ' ';
    }
    ret += '(';
    ret += tostr(k, j, y, false);
    ret += ')';
  } else if (t == 1 || t == 2) {
    ret = tostr(i, k, x, space) + tostr(k, j, y, true);
  } else if (t == 5) {
    if (space) {
      ret.resize(2);
      ret[0] = ' ';
      ret[1] = k;
    } else {
      ret.assign(1, k);
    }
  }

  return ret;
}

string solve(string s) {
  memset(cnt, 0, sizeof cnt);
  memset(dat, 0, sizeof dat);
  memset(lft, 0, sizeof lft);
  memset(rgt, 0, sizeof rgt);

  s.erase(remove_if(s.begin(), s.end(), unnec), s.end());

  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == 'l') {
      cnt[i][i + 1][3] = 1;
    } else if (s[i] == 'n') {
      cnt[i][i + 1][4] = 1;
    } else {
      cnt[i][i + 1][5] = 1;
      dat[i][i + 1][5] = s[i];
    }
  }

  int n = s.size();
  for (int len = 2; len <= n; ++len) {
    for (int i = 0; i + len <= n; ++i) {
      int j = i + len;
      for (int k = i; k < i + len; ++k) {
        for (int x = 0; x < 6; ++x) {
          if (cnt[i][k][x] == 0) {
            continue;
          }
          for (int y = 0; y < 6; ++y) {
            int t = tbl[x][y];
            if (cnt[k][j][y] == 0 || t == -1) {
              continue;
            }
            if (++cnt[i][j][t] == 1) {
              dat[i][j][t] = k;
              lft[i][j][t] = x;
              rgt[i][j][t] = y;
            }
          }
        }
      }
    }
  }

  if (cnt[0][n][0] == 1) {
    try {
      return tostr(0, n, 0, false);
    } catch (...) {
    }
  }

  return "AMBIGUOUS";
}

int main() {
  int n;
  cin >> n;
  cin.get();
  string s;
  for (int i = 0; i < n; ++i) {
    getline(cin, s);
    cout << solve(s) << '\\n';
  }
}