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

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
#define A first.first
#define B first.second.first
#define C first.second.second
#define M make_pair
#define N 10
#define P push_back
using namespace std;
typedef pair<int, int> P1;
typedef pair<int, P1> P2;
typedef map<P2, int> MAP;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int m, int n) {
  if ((0 == m) || (0 == n))
    return 0;
  return (m / gcd(m, n)) * n;
}
void cal();
int R(char);
int cnt[2][N][3], n[2], ans, X, Y, Z, fa, fb, fc;
string st[2][N];
vector<pair<char, int>> v;
MAP m;

int main() {
  while (1) {
    cin >> n[0] >> n[1];
    if (!n[0] && !n[1])
      break;
    for (int i = 0; i < 2; i++)
      for (int j = 0; j < N; j++)
        for (int k = 0; k < 3; k++)
          cnt[i][j][k] = 0;
    for (int i = 0; i < 2; i++)
      for (int j = 0; j < n[i]; j++)
        cin >> st[i][j];
    for (int x = 0; x < 2; x++)
      for (int i = 0; i < n[x]; i++)
        for (int j = 0; j < st[x][i].size(); j++) {
          char c = st[x][i][j];
          if (c == '(' || c == '{' || c == '[')
            v.P(M(c, i));
          if (c == ')' || c == '}' || c == ']') {
            int ind = v.size() - 1;
            if (v[ind].second != i)
              for (int k = v[ind].second + 1; k <= i; k++)
                cnt[x][k][R(c)]++;
            v.erase(v.begin() + ind);
          }
        }
    for (int i = 1; i < n[0]; i++)
      for (int j = 0; j < st[0][i].size(); j++)
        if (st[0][i][j] != '.') {
          m.insert(M(M(cnt[0][i][0], M(cnt[0][i][1], cnt[0][i][2])), j));
          break;
        }
    X = Y = Z = 0;
    cal();
    cout << 0;
    for (int i = 1; i < n[1]; i++) {
      fa = cnt[1][i][0], fb = cnt[1][i][1], fc = cnt[1][i][2];
      if (i)
        cout << ' ';
      ans = -1;
      for (MAP::iterator j = m.begin(); j != m.end(); j++) {
        int a = (*j).A, b = (*j).B, c = (*j).C;
        if (a && b && c && ((*j).second == 3 || (*j).second == 60))
          X = (*j).second / 3, Y = X, Z = X;
      }
      if (fa && fb && fc && X && Y && Z)
        ans = X * fa + Y * fb + Z * fc;
      if (fa && fb && !fc) {
        if (X && Y)
          ans = X * fa + Y * fb;
        else
          for (MAP::iterator j = m.begin(); j != m.end(); j++) {
            int a = (*j).A, b = (*j).B, c = (*j).C, d = 0;
            if (a && b) {
              d = (*j).second;
              if (c && Z)
                d -= c * Z;
              int r = gcd(a, b);
              a /= r;
              b /= r;
              r = gcd(fa, fb);
              if (fa / r == a && fb / r == b)
                ans = d * (fa / a);
            }
          }
      }
      if (fa && !fb && fc) {
        if (X && Z)
          ans = X * fa + Z * fc;
        else
          for (MAP::iterator j = m.begin(); j != m.end(); j++) {
            int a = (*j).A, b = (*j).B, c = (*j).C, d = 0;
            if (c && a) {
              d = (*j).second;
              if (b && Y)
                d -= b * Y;
              int r = gcd(c, a);
              c /= r;
              a /= r;
              r = gcd(fc, fa);
              if (fc / r == c && fa / r == a)
                ans = d * (fc / c);
            }
          }
      }
      if (!fa && fb && fc) {
        if (Y && Z)
          ans = Y * fb + Z * fc;
        else
          for (MAP::iterator j = m.begin(); j != m.end(); j++) {
            int a = (*j).A, b = (*j).B, c = (*j).C, d = 0;
            if (b && c) {
              d = (*j).second;
              int r = gcd(b, c);
              if (a && X)
                d -= a * X;
              b /= r;
              c /= r;
              r = gcd(fb, fc);
              if (fb / r == b && fc / r == c)
                ans = d * (fb / b);
            }
          }
      }
      if (fa && !fb && !fc && X)
        ans = X * fa;
      if (!fa && fb && !fc && Y)
        ans = Y * fb;
      if (!fa && !fb && fc && Z)
        ans = Z * fc;
      if (!fa && !fb && !fc)
        ans = 0;
      cout << ans;
    }
    cout << endl;
    m.clear();
  }
  return 0;
}

int R(char c) {
  int r = 2;
  if (c == ')')
    r = 0;
  if (c == '}')
    r = 1;
  return r;
}

void cal() {
  for (MAP::iterator i = m.begin(); i != m.end(); i++) {
    for (MAP::iterator j = m.begin(); j != m.end(); j++) {
      for (MAP::iterator k = m.begin(); k != m.end(); k++) {
        int r, x = -1, y = -1, z = -1;
        int a = (*i).A, b = (*i).B, c = (*i).C, d = (*i).second;
        int e = (*j).A, f = (*j).B, g = (*j).C, h = (*j).second;
        int l = (*k).A, o = (*k).B, p = (*k).C, q = (*k).second;
        if (a && e) {
          r = lcm(a, e);
          int x1 = r / a, x2 = r / e;
          a = a * x1 - e * x2;
          b = b * x1 - f * x2;
          c = c * x1 - g * x2;
          d = d * x1 - h * x2;
        }
        if (e && l) {
          r = lcm(e, l);
          int x1 = r / e, x2 = r / l;
          e = e * x1 - l * x2;
          f = f * x1 - o * x2;
          g = g * x1 - p * x2;
          h = h * x1 - q * x2;
        }
        if (l && a) {
          r = lcm(l, a);
          int x1 = r / l, x2 = r / a;
          l = l * x1 - a * x2;
          o = o * x1 - b * x2;
          p = p * x1 - c * x2;
          q = q * x1 - d * x2;
        }
        if (b && f) {
          r = lcm(b, f);
          int x1 = r / b, x2 = r / f;
          if (a >= e)
            a = a * x1 - e * x2, b = b * x1 - f * x2, c = c * x1 - g * x2,
            d = d * x1 - h * x2;
          else
            e = e * x2 - a * x1, f = f * x2 - b * x1, g = g * x2 - c * x1,
            h = h * x2 - d * x1;
        }
        if (f && o) {
          r = lcm(f, o);
          int x1 = r / f, x2 = r / o;
          if (e >= l)
            e = e * x1 - l * x2, f = f * x1 - o * x2, g = g * x1 - p * x2,
            h = h * x1 - q * x2;
          else
            l = l * x2 - e * x1, o = o * x2 - f * x1, p = p * x2 - g * x1,
            q = q * x2 - h * x1;
        }
        if (o && b) {
          r = lcm(o, b);
          int x1 = r / o, x2 = r / b;
          if (l >= a)
            l = l * x1 - a * x2, o = o * x1 - b * x2, p = p * x1 - c * x2,
            q = q * x1 - d * x2;
          else
            a = a * x2 - l * x1, b = b * x2 - o * x1, c = c * x2 - p * x1,
            d = d * x2 - q * x1;
        }
        if (!a && !b) {
          if (g) {
            if (c) {
              r = lcm(c, g);
              int x1 = r / c, x2 = r / g;
              g = g * x2 - c * x1, h = h * x2 - d * x1;
            }
          } else if (p) {
            if (c) {
              r = lcm(c, p);
              int x1 = r / c, x2 = r / p;
              p = p * x2 - c * x1, q = q * x2 - d * x1;
            }
          }
        }
        if (!e && !f) {
          if (p) {
            if (g) {
              r = lcm(g, p);
              int x1 = r / g, x2 = r / p;
              p = p * x2 - g * x1, q = q * x2 - h * x1;
            }
          } else if (c) {
            if (g) {
              r = lcm(g, c);
              int x1 = r / g, x2 = r / c;
              c = c * x2 - g * x1, d = d * x2 - h * x1;
            }
          }
        }
        if (!l && !o) {
          if (c) {
            if (p) {
              r = lcm(p, c);
              int x1 = r / p, x2 = r / c;
              c = c * x2 - p * x1, d = d * x2 - q * x1;
            }
          } else if (g) {
            if (p) {
              r = lcm(p, g);
              int x1 = r / p, x2 = r / g;
              g = g * x2 - p * x1, h = h * x2 - q * x1;
            }
          }
        }
        if (a && !b && !c && !e && !l)
          x = d / a;
        if (e && !f && !g && !a && !l)
          x = h / e;
        if (l && !o && !p && !a && !e)
          x = q / l;
        if (!a && b && !c && !f && !o)
          y = d / b;
        if (!e && f && !g && !b && !o)
          y = h / f;
        if (!l && o && !p && !b && !f)
          y = q / o;
        if (!a && !b && c && !g && !p)
          z = d / c;
        if (!e && !f && g && !c && !p)
          z = h / g;
        if (!l && !o && p && !c && !g)
          z = q / p;
        if (x >= 0)
          X = x;
        if (y >= 0)
          Y = y;
        if (z >= 0)
          Z = z;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <vector>
#define A first.first
#define B first.second.first
#define C first.second.second
#define M make_pair
#define N 10
#define P push_back
using namespace std;
typedef pair<int, int> P1;
typedef pair<int, P1> P2;
typedef map<P2, int> MAP;
void Find(int, int, int, int, int, int);
void UpDate();
int R(char);
int cnt[2][N][3], n[2], ans, fa, fb, fc, fd, flag;
string st[2][N];
vector<pair<char, int>> v;
MAP m;

int main() {
  while (1) {
    cin >> n[0] >> n[1];
    if (!n[0] && !n[1])
      break;
    for (int i = 0; i < 2; i++)
      for (int j = 0; j < N; j++)
        for (int k = 0; k < 3; k++)
          cnt[i][j][k] = 0;
    for (int i = 0; i < 2; i++)
      for (int j = 0; j < n[i]; j++)
        cin >> st[i][j];
    for (int x = 0; x < 2; x++)
      for (int i = 0; i < n[x]; i++)
        for (int j = 0; j < st[x][i].size(); j++) {
          char c = st[x][i][j];
          if (c == '(' || c == '{' || c == '[')
            v.P(M(c, i));
          if (c == ')' || c == '}' || c == ']') {
            int ind = v.size() - 1;
            if (v[ind].second != i)
              for (int k = v[ind].second + 1; k <= i; k++)
                cnt[x][k][R(c)]++;
            v.erase(v.begin() + ind);
          }
        }
    for (int i = 1; i < n[0]; i++)
      for (int j = 0; j < st[0][i].size(); j++)
        if (st[0][i][j] != '.') {
          m.insert(M(M(cnt[0][i][0], M(cnt[0][i][1], cnt[0][i][2])), j));
          break;
        }
    //    for(int i=0;i<n[0];i++) cout<<cnt[0][i][0]<<' '<<cnt[0][i][1]<<'
    //    '<<cnt[1][i][2]<<endl; cout<<endl;
    UpDate();
    cout << 0;
    for (int i = 1; i < n[1]; i++) {
      fa = cnt[1][i][0], fb = cnt[1][i][1], fc = cnt[1][i][2];
      if (i)
        cout << ' ';
      flag = 0;
      ans = -1;
      // Find(0,0,0,0,-1,0);
      if (ans < 0)
        cout << -1;
      else
        cout << ans;
    }
    cout << endl;
    m.clear();
  }
  return 0;
}

void UpDate() {

  for (MAP::iterator i = m.begin(); i != m.end(); i++) {
    for (MAP::iterator j = m.begin(); j != m.end(); j++) {
      if (*i == *j)
        continue;
      if ((*i).A > (*j).A || (*i).B > (*j).B || (*i).C > (*j).C)
        continue;
      int a = (*j).A, b = (*j).B, c = (*j).C;
      while (a - (*i).A >= 0 && b - (*i).B >= 0 && c - (*i).C >= 0)
        a -= (*i).A, b -= (*i).B, c -= (*i).C;
      if (m.find(M(a, M(b, c))) != m.end())
        continue;
      m.insert(M(M(a, M(b, c)), (*j).second - (*i).second));
    }
  }
  for (MAP::iterator i = m.begin(); i != m.end(); i++) {
    int a = (*i).A, b = (*i).B, c = (*i).C, d = (*i).second;
    if (a && !b && !c)
      m.insert(M(M(1, M(0, 0)), d / a));
    if (!a && b && !c)
      m.insert(M(M(0, M(1, 0)), d / b));
    if (!a && !b && c)
      m.insert(M(M(0, M(0, 1)), d / c));
    if (m.size() == 1 && a == 1 && b == 1 && c == 1) {
      m.insert(M(M(1, M(0, 0)), d / 3));
      m.insert(M(M(0, M(1, 0)), d / 3));
      m.insert(M(M(0, M(0, 1)), d / 3));
    }
  }
}

int R(char c) {
  int r = 2;
  if (c == ')')
    r = 0;
  if (c == '}')
    r = 1;
  return r;
}