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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X)                                                         \\
  for (__typeof((X).begin()) it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)x.size())

using namespace std;

struct timer {
  time_t start;
  timer() { start = clock(); }
  ~timer() {
    cerr << 1. * (clock() - start) / CLOCKS_PER_SEC << " secs" << endl;
  }
};

typedef istringstream iss;
typedef long long ll;
typedef pair<int, int> pi;
typedef stringstream sst;
typedef vector<int> vi;

struct tree {
  bool leaf;
  tree *left, *right;
  int size;
} *root;

string s;
int cur = 0;
int n;
int depth = 0, deepest = -1;
tree *dnode;
vector<tree *> cand;
vi parts;

tree *parse() {
  tree *res = new tree;
  if (depth > deepest) {
    deepest = depth;
    dnode = res;
  }
  assert(s[cur] == '(');
  if (s[cur + 1] == ')') {
    res->leaf = 1;
    res->size = 0;
    cur += 2;
    return res;
  }
  res->leaf = 0;
  res->size = 1;
  cur++;
  depth++;
  res->left = parse();
  res->size += res->left->size;
  res->right = parse();
  res->size += res->right->size;
  assert(s[cur] == ')');
  cur++;
  depth--;
  return res;
}

bool markcand(tree *node) {
  if (node->leaf) {
    return node == dnode;
  }
  if (markcand(node->left) || markcand(node->right)) {
    if (n % node->size == 0) {
      cand.pb(node);
    }
    return 1;
  }
  return 0;
}

bool match(tree *node, int id, tree *node2) {
  if (node->leaf && node2->leaf) {
    return 1;
  }
  if (node->leaf && !node2->leaf) {
    return 0;
  }
  if (!node->leaf && node2->leaf) {
    return match(node->left, id, cand[id]->left) &&
           match(node->right, id, cand[id]->right);
  }
  if (!node->leaf && !node2->leaf) {
    return match(node->left, id, node2->left) &&
           match(node->right, id, node2->right);
  }
  return 0;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);

  cin >> s;
  root = parse();
  n = root->size;
  markcand(root);
  rep(i, sz(cand)) {
    if (match(root, i, cand[i])) {
      parts.pb(cand[i]->size);
    }
  }
  vi dp(sz(parts), INF);
  dp[0] = 0;
  rep2(i, 1, sz(parts)) {
    rep(j, i) if (parts[i] % parts[j] == 0) {
      dp[i] = min(dp[i], dp[j] + parts[i] / parts[j] - 1);
    }
  }
  cout << dp[sz(parts) - 1] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X)                                                         \\
  for (__typeof((X).begin()) it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)x.size())

using namespace std;

struct timer {
  time_t start;
  timer() { start = clock(); }
  ~timer() {
    cerr << 1. * (clock() - start) / CLOCKS_PER_SEC << " secs" << endl;
  }
};

typedef istringstream iss;
typedef long long ll;
typedef pair<int, int> pi;
typedef stringstream sst;
typedef vector<int> vi;

struct tree {
  bool leaf;
  tree *left, *right;
  int size;
} *root;

string s;
int cur = 0;
int n;
int depth = 0, deepest = -1;
tree *dnode;
vector<tree *> cand;
vi parts;

tree *parse() {
  tree *res = new tree;
  if (depth > deepest) {
    deepest = depth;
    dnode = res;
  }
  assert(s[cur] == '(');
  if (s[cur + 1] == ')') {
    res->leaf = 1;
    res->size = 0;
    cur += 2;
    return res;
  }
  res->leaf = 0;
  res->size = 1;
  cur++;
  depth++;
  res->left = parse();
  res->size += res->left->size;
  res->right = parse();
  res->size += res->right->size;
  assert(s[cur] == ')');
  cur++;
  depth--;
  return res;
}

bool markcand(tree *node) {
  if (node->leaf) {
    return node == dnode;
  }
  if (markcand(node->left) || markcand(node->right)) {
    if (n % node->size == 0) {
      cand.pb(node);
    }
    return 1;
  }
  return 0;
}

bool match(tree *node, int id, tree *node2) {
  if (node->leaf && node2->leaf) {
    return 1;
  }
  if (node->leaf && !node2->leaf) {
    return 0;
  }
  if (!node->leaf && node2->leaf) {
    return match(node->left, id, cand[id]->left) &&
           match(node->right, id, cand[id]->right);
  }
  if (!node->leaf && !node2->leaf) {
    return match(node->left, id, node2->left) &&
           match(node->right, id, node2->right);
  }
  return 0;
}

void show(tree *tr) {
  if (tr->leaf == 1) {
    cout << "()";
    return;
  }
  cout << "(";
  show(tr->left);
  show(tr->right);
  cout << ")";
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);

  cin >> s;
  root = parse();
  markcand(root);
  n = root->size;
  rep(i, sz(cand)) {
    if (match(root, i, cand[i])) {
      parts.pb(cand[i]->size);
    }
  }
  assert(sz(cand) < 1000);
  vi dp(sz(parts), INF);
  dp[0] = 0;
  rep2(i, 1, sz(parts)) {
    rep(j, i) if (parts[i] % parts[j] == 0) {
      dp[i] = min(dp[i], dp[j] + parts[i] / parts[j] - 1);
    }
  }
  cout << dp[sz(parts) - 1] << endl;
}