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

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;

int main() {
  int n;
  while (cin >> n, n) {
    int ans = 0;
    int ma = 0;
    vector<P> dat(n);
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      dat[i].first = (int)s.size() - 1;
      if (s[s.size() - 1] == '+')
        dat[i].second = -1;
      else if (s[s.size() - 1] == '*')
        dat[i].second = -2;
      else
        dat[i].second = s[s.size() - 1] - '0';
      ma = max(ma, (int)s.size() - 1);
    }

    while (1) {
      /*for ( int i = 0; i < dat.size(); i++ ) {
        if ( i ) cout << " ";
        cout << dat[i].first << " " << dat[i].second;
      }
      cout << endl << endl;;*/
      if (dat.size() == 1)
        break;
      bool flag = false;
      for (int i = 0; i < dat.size() - 2; i++) {
        if (dat[i].second >= 0)
          continue;
        if (dat[i].first != ma)
          continue;
        int c = dat[i].second;
        int d = dat[i].first;
        int sum;
        int cnt = 2;
        if (d != dat[i + 1].first - 1)
          continue;
        if (dat[i + 1].second >= 0 && dat[i + 2].second >= 0 &&
            dat[i + 1].first == dat[i + 2].first) {
          flag = true;
          // cout << 'a' << endl;
          sum = dat[i + 1].second;
          cnt = 2;
          for (int j = i + 2; j < dat.size(); j++) {
            // cout << sum << endl;
            if (dat[j].second < 0)
              break;
            if (dat[j].first != dat[i + 1].first)
              break;
            if (c == -1) {
              sum += dat[j].second;
            } else {
              sum *= dat[j].second;
            }
            cnt++;
          }
        }
        if (flag) {
          // cout << i << " " << sum << " " << cnt << endl;
          int tmp = dat[i].first;
          for (int j = i; j < i + cnt; j++)
            dat.erase(dat.begin() + i);
          dat.insert(dat.begin() + i, P(tmp, sum));
          break;
        }
      }
      if (!flag) {
        ma--;
      }

      if (dat.size() == 1)
        break;
    }

    // cout << "end" << endl;
    cout << dat[0].second << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;

int main() {
  int n;
  while (cin >> n, n) {
    int ans = 0;
    vector<P> dat(n);
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      dat[i].first = (int)s.size() - 1;
      if (s[s.size() - 1] == '+')
        dat[i].second = -1;
      else if (s[s.size() - 1] == '*')
        dat[i].second = -2;
      else
        dat[i].second = s[s.size() - 1] - '0';
    }

    while (1) {
      /*for ( int i = 0; i < dat.size(); i++ ) {
        if ( i ) cout << " ";
        cout << dat[i].first << " " << dat[i].second;
      }
      cout << endl << endl;;*/
      if (dat.size() == 1)
        break;
      for (int i = 0; i < dat.size() - 2; i++) {
        if (dat[i].second >= 0)
          continue;
        int c = dat[i].second;
        int d = dat[i].first;
        int sum;
        int cnt = 2;
        bool flag = false;
        if (d != dat[i + 1].first - 1)
          continue;
        if (dat[i + 1].second >= 0 && dat[i + 2].second >= 0 &&
            dat[i + 1].first == dat[i + 2].first) {
          flag = true;
          // cout << 'a' << endl;
          sum = dat[i + 1].second;
          cnt = 2;
          for (int j = i + 2; j < dat.size(); j++) {
            // cout << sum << endl;
            if (dat[j].second < 0)
              break;
            if (dat[j].first != dat[i + 1].first)
              break;
            if (c == -1) {
              sum += dat[j].second;
            } else {
              sum *= dat[j].second;
            }
            cnt++;
          }
        }
        if (flag) {
          // cout << i << " " << sum << " " << cnt << endl;
          int tmp = dat[i].first;
          for (int j = i; j < i + cnt; j++)
            dat.erase(dat.begin() + i);
          dat.insert(dat.begin() + i, P(tmp, sum));
          break;
        }
      }
      if (dat.size() == 1)
        break;
    }

    // cout << "end" << endl;
    cout << dat[0].second << endl;
  }
}