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

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <iostream>

using namespace std;

string tree[2];

string uni(int index0, int index1) {
  string left = "", right = "";
  if (tree[0][index0 + 1] == ',') {
    int c = 0;
    index0++, index1++;
    if (tree[1][index1] != ',')
      while (true) {
        if (c == 0 && tree[1][index1] == ',') {
          break;
        }
        if (tree[1][index1] == '(') {
          c++;
        }
        if (tree[1][index1] == ')') {
          c--;
        }
        left = left + tree[1][index1];
        index1++;
      }
  } else if (tree[1][index1 + 1] == ',') {
    int c = 0;
    index0++, index1++;
    while (true) {
      if (c == 0 && tree[0][index0] == ',') {
        break;
      }
      if (tree[0][index0] == '(') {
        c++;
      }
      if (tree[0][index0] == ')') {
        c--;
      }
      left = left + tree[0][index0];
      index0++;
    }
  } else {
    left = uni(index0 + 1, index1 + 1);
    int c = 0;
    index0++, index1++;
    while (true) {
      //      cout << tree[0][index0];
      if (c == 0 && tree[0][index0] == ',') {
        break;
      }
      if (tree[0][index0] == '(') {
        c++;
      }
      if (tree[0][index0] == ')') {
        c--;
      }
      index0++;
    }
    c = 0;
    while (true) {
      //      cout << tree[1][index1];
      if (c == 0 && tree[1][index1] == ',') {
        break;
      }
      if (tree[1][index1] == '(') {
        c++;
      }
      if (tree[1][index1] == ')') {
        c--;
      }
      index1++;
    }
  }

  if (tree[0][index0 + 1] == ')') {
    int c = 0;
    index1++;
    while (true) {
      if (c == 0 && tree[1][index1] == ')') {
        break;
      }
      if (tree[1][index1] == '(') {
        c++;
      }
      if (tree[1][index1] == ')') {
        c--;
      }
      right = right + tree[1][index1];
      index1++;
    }
  } else if (tree[1][index1 + 1] == ')') {
    int c = 0;
    index0++;
    while (true) {
      if (c == 0 && tree[0][index0] == ')') {
        break;
      }
      if (tree[0][index0] == '(') {
        c++;
      }
      if (tree[0][index0] == ')') {
        c--;
      }
      right = right + tree[0][index0];
      index0++;
    }
  } else {
    right = uni(index0 + 1, index1 + 1);
  }
  return "(" + left + "," + right + ")";
}

string inter(int index0, int index1) {
  string left = "", right = "";
  if (tree[0][index0 + 1] == ',') {
    index0++, index1++;
    int c = 0;
    while (true) {
      if (c == 0 && tree[1][index1] == ',') {
        break;
      }
      if (tree[1][index1] == '(') {
        c++;
      }
      if (tree[1][index1] == ')') {
        c--;
      }
      index1++;
    }
  } else if (tree[1][index1 + 1] == ',') {
    index0++, index1++;
    int c = 0;
    while (true) {
      if (c == 0 && tree[0][index0] == ',') {
        break;
      }
      if (tree[0][index0] == '(') {
        c++;
      }
      if (tree[0][index0] == ')') {
        c--;
      }
      index0++;
    }
  } else {
    left = inter(index0 + 1, index1 + 1);
    int c = 0;
    index0++, index1++;
    while (true) {
      if (c == 0 && tree[1][index1] == ',') {
        break;
      }
      if (tree[1][index1] == '(') {
        c++;
      }
      if (tree[1][index1] == ')') {
        c--;
      }
      index1++;
    }
    c = 0;
    while (true) {
      if (c == 0 && tree[0][index0] == ',') {
        break;
      }
      if (tree[0][index0] == '(') {
        c++;
      }
      if (tree[0][index0] == ')') {
        c--;
      }
      index0++;
    }
  }

  if (tree[0][index0 + 1] == ')' || tree[1][index1 + 1] == ')') {
    right = "";
  } else {
    right = inter(index0 + 1, index1 + 1);
  }

  return "(" + left + "," + right + ")";
}

int main() {
  char ex;

  while (cin >> ex >> tree[0] >> tree[1]) {
    cin.ignore();
    if (ex == 'i') {
      cout << inter(0, 0) << endl;
    } else {
      cout << uni(0, 0) << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <iostream>

using namespace std;

string tree[2];

string uni(int index0, int index1) {
  string left = "", right = "";
  if (tree[0][index0 + 1] == ',') {
    int c = 0;
    index0++, index1++;
    if (tree[1][index1] != ',')
      while (true) {
        if (c == 0 && tree[1][index1] == ',') {
          break;
        }
        if (tree[1][index1] == '(') {
          c++;
        }
        if (tree[1][index1] == ')') {
          c--;
        }
        left = left + tree[1][index1];
      }
  } else if (tree[1][index1 + 1] == ',') {
    int c = 0;
    index0++, index1++;
    while (true) {
      if (c == 0 && tree[0][index0] == ',') {
        break;
      }
      if (tree[0][index0] == '(') {
        c++;
      }
      if (tree[0][index0] == ')') {
        c--;
      }
      left = left + tree[0][index0];
    }
  } else {
    left = uni(index0 + 1, index1 + 1);
    int c = 0;
    index0++, index1++;
    while (true) {
      //      cout << tree[0][index0];
      if (c == 0 && tree[0][index0] == ',') {
        break;
      }
      if (tree[0][index0] == '(') {
        c++;
      }
      if (tree[0][index0] == ')') {
        c--;
      }
      index0++;
    }
    c = 0;
    while (true) {
      //      cout << tree[1][index1];
      if (c == 0 && tree[1][index1] == ',') {
        break;
      }
      if (tree[1][index1] == '(') {
        c++;
      }
      if (tree[1][index1] == ')') {
        c--;
      }
      index1++;
    }
  }

  if (tree[0][index0 + 1] == ')') {
    int c = 0;
    index1++;
    while (true) {
      if (c == 0 && tree[1][index1] == ')') {
        break;
      }
      if (tree[1][index1] == '(') {
        c++;
      }
      if (tree[1][index1] == ')') {
        c--;
      }
      right = right + tree[1][index1];
      index1++;
    }
  } else if (tree[1][index1 + 1] == ')') {
    int c = 0;
    index0++;
    while (true) {
      if (c == 0 && tree[0][index0] == ')') {
        break;
      }
      if (tree[0][index0] == '(') {
        c++;
      }
      if (tree[0][index0] == ')') {
        c--;
      }
      right = right + tree[0][index0];
      index0++;
    }
  } else {
    right = uni(index0 + 1, index1 + 1);
  }
  return "(" + left + "," + right + ")";
}

string inter(int index0, int index1) {
  string left = "", right = "";
  if (tree[0][index0 + 1] == ',') {
    index0++, index1++;
    int c = 0;
    while (true) {
      if (c == 0 && tree[1][index1] == ',') {
        break;
      }
      if (tree[1][index1] == '(') {
        c++;
      }
      if (tree[1][index1] == ')') {
        c--;
      }
      index1++;
    }
  } else if (tree[1][index1 + 1] == ',') {
    index0++, index1++;
    int c = 0;
    while (true) {
      if (c == 0 && tree[0][index0] == ',') {
        break;
      }
      if (tree[0][index0] == '(') {
        c++;
      }
      if (tree[0][index0] == ')') {
        c--;
      }
      index0++;
    }
  } else {
    left = inter(index0 + 1, index1 + 1);
    int c = 0;
    index0++, index1++;
    while (true) {
      if (c == 0 && tree[1][index1] == ',') {
        break;
      }
      if (tree[1][index1] == '(') {
        c++;
      }
      if (tree[1][index1] == ')') {
        c--;
      }
      index1++;
    }
    c = 0;
    while (true) {
      if (c == 0 && tree[0][index0] == ',') {
        break;
      }
      if (tree[0][index0] == '(') {
        c++;
      }
      if (tree[0][index0] == ')') {
        c--;
      }
      index0++;
    }
  }

  if (tree[0][index0 + 1] == ')' || tree[1][index1 + 1] == ')') {
    right = "";
  } else {
    right = inter(index0 + 1, index1 + 1);
  }

  return "(" + left + "," + right + ")";
}

int main() {
  char ex;

  while (cin >> ex >> tree[0] >> tree[1]) {
    cin.ignore();
    if (ex == 'i') {
      cout << inter(0, 0) << endl;
    } else {
      cout << uni(0, 0) << endl;
    }
  }
  return 0;
}