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

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// ?????¢
// XCode??§???EOF??\\??????Ctrl+D
// ?\\???Alt+\\
// ans???????§?INT?????????2,147,483,647????¶????????????¨??????????????§long
// long?????£???????????????????????????
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////
// ALDS1_14_D:   Multiple String Matching
// Aho-Corasick????????????????????????MLE??????TLE?????£????????§???
// Suffix-Array????????????
////////////////////////////////////////

// #define debug
// //*******************************************************************************************************************************************
#ifdef debug
#include <chrono>
#endif

#include <algorithm> // next_permutation
#include <cmath>
#include <cstdio>
#include <cstring> //memcpy
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric> //accumulate
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// #include <map>
// #include <unordered_map> //hash func.
#include <fstream>  //ifstream, ofstream
#include <iterator> //insert_iterator::inserter

// #define NDEBUG //If NDEBUG is defined before #include <cassert>, assert will
// be ignored. You had better define NDEBUG when u submit the code.
#include <cassert> //assert

using namespace std;

#define dout cout
// If u wanna output to a text file instead of standard output, plz define
// OUTPUTFILE. #define OUTPUTFILE "output.txt"
// //*******************************************************************************************************************************************
#ifdef OUTPUTFILE
#define dout outputfile
ofstream outputfile(OUTPUTFILE);
#define OutputFilePath                                                         \\
  "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/"      \\
  "output.txt"
#endif

#define din cin
// If u wanna input from a text file instead of standard input, plz define
// INPUTFROMTEXTFILE???. #define INPUTFILE "input.txt"
// //*******************************************************************************************************************************************
#ifdef INPUTFILE
#define din inputfile
ifstream inputfile(INPUTFILE);
#endif

#define scand(A) scanf("%d", &(A))
#define scans(A) scanf("%s", (A))
#define disp(A) dout << #A << " = " << setw(3) << (A) << endl
#define disP(A) dout << setw(3) << (A) << " "
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define dispAll(A, n)                                                          \\
  dout << #A << " = ";                                                         \\
  rep(j, 0, (n)) { disP(A[j]); }                                               \\
  dout << endl
// #define dispAll(A,n) cout << #A << " = "; rep(j, 0, (n)) {cout << setw(3) <<
// A[j] << " ";} cout << endl

#define sign(x) ((x) > 0) - ((x) < 0) // x<0: -1, x=0: 0, x>0: +1
#define p(i) (i) / 2
#define l(i) (i) * 2
#define r(i) (i) * 2 + 1

int dx[] = {
    1, -1, 0, 0, 1,
    1, -1, -1}; //???????????????????????????????¨??????????????????????????????????
int dy[] = {0, 0, -1, 1, -1, 1, 1, -1};

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long ull;

const int INF = (int)2e9 + 10;
const ll INF_LL = (ll)9e18 - 1LL; // Be careful for overflow.
const ull INF_ULL = (ull)1e19 - 1ULL;
const int NONE = -1;
const ll MOD =
    (ll)1e9 + 7; //??§???????´???°?????£??¨??¨????????°????????????10???7??????

const int N_MAX = 20010; // num of vertex or element
const int M_MAX = 10010; // num of edge
const int DATA_MAX = 1010;

int N;

int i;
char S[N_MAX];

int height = 0;
int area = 0;
stack<int> backslashStack;
vector<pii> floodAreaArray;

#ifdef debug
#endif

#ifdef debug

void display() {

  disp(area);
  disp(height);

  disp(backslashStack.size());
  disp(floodAreaArray.size());

  dout << endl;

  dout << "floodAreaArray = ";
  rep(i, 0, floodAreaArray.size()) dout
      << "( " << floodAreaArray[i].first << ", "
      << (floodAreaArray[i].second == INF ? 999 : floodAreaArray[i].second)
      << " ) , ";

  dout << endl;
}
#endif

int main() {

  // cin, cout????????????
  // ?????¨??????cin?????????????????¨??¨cin??§???scanf?????????????????¨??¨scanf??§??±?????????????????????
  cin.tie(0);                  // cin??¨cout??????????????????
  ios::sync_with_stdio(false); // iostream??¨stdio??????????????????

  // read input data
  scans(S);

  //------------------------------------------------------------------------------------------
#ifdef debug
  // start timer
  auto startTime = chrono::system_clock::now();
#endif
  //------------------------------------------------------------------------------------------

  strcat(S, "\\\\"); // sentinel
  N = strlen(S);

#ifdef debug
  disp(S);
  disp(N);
#endif

  // calc.
  for (i = 0; i < N; i++) {
#ifdef debug
    dout << "-----\\n";
    disp(i);
    disp(S[i]);
#endif

    if (S[i] == '\\\\') {

      if (area != 0) {
        if (backslashStack
                .empty()) { // area???????¢?????????????????????????????????\\??????????´??°´??¨?????¢??????????????????????????¨???????¨??????????
#ifdef debug
          dout << "floodAreaArray.push_back( " << area << ", h = " << 999
               << ")\\n";
#endif
          floodAreaArray.push_back(make_pair(area, INF));
        }

        else { // area??????????¢??????????????????????area???????????\\??????????´??°´??¨?????¢??????????????????????????§?????????
#ifdef debug
          dout << "floodAreaStack.push_back( area = " << area
               << ", h = " << height << ")\\n";
#endif
          floodAreaArray.push_back(make_pair(area, height));
        }

        area = 0; //??????????????????area???????????????
      }

      height--;
#ifdef debug
      dout << "backslashStack.push_back( " << i << " )\\n";
#endif
      backslashStack.push(i);
    }

    else if (S[i] == '/') {

      height++;

      if (not backslashStack.empty()) {
#ifdef debug
        dout << "area += " << i << " - " << backslashStack.top() << "\\n";
        dout << "backslashStack.pop()\\n";
#endif
        area += i - backslashStack.top();
        backslashStack.pop();

        //????¨??????????????????°???????´??°´??¨?????¢?????????
        while (not floodAreaArray.empty() and
               floodAreaArray.back().second < height) {
#ifdef debug
          dout << "floodAreaArray.back() = (" << floodAreaArray.back().first
               << ", " << floodAreaArray.back().second << ")\\n";
          dout << "< height = " << height << "\\n";
          dout << "area += " << floodAreaArray.back().first << "\\n";
          dout << "erase floodAreaArray.back()\\n";
#endif
          area += floodAreaArray.back().first;
          floodAreaArray.erase(floodAreaArray.end() - 1);
        }
      }

    }

    else {
      // no action for S[i]=='_'
    }

#ifdef debug
    display();
#endif
  }

  // output
  int totalFloodArea = 0;
  int numOfFloodArea = floodAreaArray.size();
  rep(i, 0, numOfFloodArea) totalFloodArea += floodAreaArray[i].first;

  printf("%d\\n%d", totalFloodArea, numOfFloodArea);
  rep(i, 0, numOfFloodArea) printf(" %d", floodAreaArray[i].first);
  puts("");

#ifdef debug
  dout << "Please input again.\\n";

  string landmap;
  cin >> landmap;
  //        dout << landmap << endl << "********";

  N = landmap.size();

  int currentHeight = 0;
  double floodArea;
  int depth;

  vector<double> floodAreaData; //????´??°´??°????????¢??????????´???????

  int i, j;

  // calc.
  for (i = 0; i < N; i++) {
    dout << "i=" << i << " ";
    if (landmap[i] ==
        '\\\\') { //??????????????°??¢?????????????????????????°´??????????????§???????????????????????\\???
      dout << "\\\\ ";
      depth = 1;
      floodArea = 0.5;

      for (j = i + 1; j < N && depth != 0;
           j++) { // i???????°´??????????????§????????´????????¢???????¨????
        if (landmap[j] == '\\\\') {
          floodArea += depth + 0.5;
          depth++;
        } else if (landmap[j] == '_') {
          floodArea += depth;
        } else if (landmap[j] == '/') {
          depth--;
          floodArea += depth + 0.5;
        }
      } // end of j-loop

      dout << "j=" << j << " floodArea=" << floodArea << " depth=" << depth;

      if (depth == 0) { //?´??°´??¨?????¢????????????
        floodAreaData.push_back(
            floodArea); //?????????????´??°´??¨?????¢?????¢????????????

        i = j -
            1; // j?????§i-loop????£???°??????i++????????????????????§j???1????¨????
      }
    }

    dout << endl;

  } // end of i-loop

  //    if(start==-1) {
  //    //?????????????????????????????????????°´??????????????§????????????
  //        //???????????????????????????floodAreaData???empty??§???????????????????????§??£????????????????????????
  //    }

  // final output
  cout << accumulate(floodAreaData.begin(), floodAreaData.end(), 0)
       << endl;                 //?????¢???
  cout << floodAreaData.size(); //?°´??????????????°

  for (auto v : floodAreaData) {
    cout << " " << v;
  }
  cout << endl;

  int landHeight[N + 1]; //????????????N+1???????????¨???

  // calc. landHeight[]???????????¨????¨?????????????
  landHeight[0] = 0;
  for (int i = 0; i < N; i++) {
    if (landmap[i] == '\\\\') {
      currentHeight--;
    } else if (landmap[i] == '/') {
      currentHeight++;
    } else if (landmap[i] == '_') {
    }

    landHeight[i + 1] = currentHeight;
  }

  //        //display i & landHeight[]
  //        for(int i=0; i<N+1; i++) {
  //            dout << setw(2) << i << " ";
  //        }
  //        dout << endl;
  //        for(int i=0; i<N+1; i++) {
  //            dout << setw(2) << landHeight[i] << " ";
  //        }
  //        dout << endl;

  //?????§????°?????±???????
  int min = *min_element(landHeight, landHeight + N + 1);
  int max = *max_element(landHeight, landHeight + N + 1);

  dout << "min = " << min << " : max = " << max << endl;

  // print cross-section diagram ????§?Ver.
  dout << "xxx";
  for (int i = 0; i < N + 1; i++) {
    dout << setw(2) << i;
  }
  dout << endl;

  for (int j = max + 1; j >= min; j--) {
    dout << setw(3) << j << " ";
    for (int i = 0; i < N + 1; i++) {
      if (landHeight[i] == j && landmap[i] == '\\\\') {
        dout << "\\\\";
      } else if (landHeight[i] + 1 == j && landmap[i] == '/') {
        dout << "/";
      } else if (landHeight[i] + 1 == j && landmap[i] == '_') {
        dout << "_";
      } else {
        dout << "O";
      }
      dout << " ";
    }
    dout << endl;
  }

  dout << "xxx";
  for (int i = 0; i < N + 1; i++) {
    dout << setw(2) << i;
  }
  dout << endl;

  // print cross-section diagram ??¨?§?Ver.
  for (int j = max + 1; j >= min; j--) {
    dout << setw(3) << j << " ";
    for (int i = 0; i < N + 1; i++) {
      if (landHeight[i] == j && landmap[i] == '\\\\') {
        dout << "???";
      } else if (landHeight[i] + 1 == j && landmap[i] == '/') {
        dout << "???";
      } else if (landHeight[i] + 1 == j && landmap[i] == '_') {
        dout << "???";
      } else {
        dout << "??£";
      }
      //            dout << " ";
    }
    dout << endl;
  }

  dout << "xxx";
  for (int i = 0; i < N + 1; i++) {
    dout << setw(2) << i;
  }
  dout << endl;

#endif

  //------------------------------------------------------------------------------------------
#ifdef debug
  // stop timer
  auto endTime = chrono::system_clock::now();
  auto dur = endTime - startTime;
  auto msec = chrono::duration_cast<chrono::milliseconds>(dur).count();
  dout << fixed << setprecision(4) << (double)msec / 1000 << " sec \\n";
#endif
  //------------------------------------------------------------------------------------------

#ifdef INPUTFILE
  inputfile.close();
#endif

#ifdef OUTPUTFILE
  outputfile.close();
  cout << "\\"" << OutputFilePath << "\\"" << endl;
#endif

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric> //accumulate
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
// #define TEST
// //*******************************************************************************************************************************************

//?????????????????¨??§?????\\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
#ifdef TEST
#define dout cout
#else
stringstream dummy; //???????????°??????dout?????????????????????????????????
#define dout                                                                   \\
  dummy.str("");                                                               \\
  dummy.clear(stringstream::goodbit);                                          \\
  dummy // dummy?????????????????????????????????????????¨?????¢?????¢???????????????
//???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§????????????????????????????????????
//http://d.hatena.ne.jp/linden/20060427/p1
#endif

int main(void) {
  string landmap;
  cin >> landmap;

  int N = landmap.size();

  int currentHeight = 0;
  double floodArea;
  int depth;

  vector<double> floodAreaData; //????´??°´??°????????¢??????????´???????

  int i, j;

  // calc.

  //?????????????????????????????°??¢?????¢???
  int start;

  start = -1;
  for (i = 0; i < N; i++) {
    dout << "i=" << i << " ";

    if (landmap[i] == '\\\\') {
      dout << "\\\\ ";

      start = i;

      depth = 1;
      floodArea = 0.5;

      for (j = start + 1; j < N && depth != 0; j++) {
        if (landmap[j] == '\\\\') {
          floodArea += depth + 0.5;
          depth++;
        } else if (landmap[j] == '_') {
          floodArea += depth;
        } else if (landmap[j] == '/') {
          depth--;
          floodArea += depth + 0.5;
        }
      } // end of j-loop

      dout << "j=" << j << " floodArea=" << floodArea << " depth=" << depth;

      if (depth == 0) { //?´??°´??¨?????¢????????????
        floodAreaData.push_back(
            floodArea); //?????????????´??°´??¨?????¢?????¢????????????

        i = j -
            1; // j?????§i-loop????£???°??????i++????????????????????§j???1????¨????
      }
    }

    dout << endl;
  }

  if (start ==
      -1) { //?????????????????????????????????????°´??????????????§????????????
            // 0?????????
  }

  // final output
  cout << accumulate(floodAreaData.begin(), floodAreaData.end(), 0)
       << endl;                 //?????¢???
  cout << floodAreaData.size(); //?°´??????????????°

  for (auto v : floodAreaData) {
    cout << " " << v;
  }
  cout << endl;

  int landHeight[N + 1]; //????????????N+1???????????¨???

  // calc. landHeight[]???????????¨????¨?????????????
  landHeight[0] = 0;
  for (int i = 0; i < N; i++) {
    if (landmap[i] == '\\\\') {
      currentHeight--;
    } else if (landmap[i] == '/') {
      currentHeight++;
    } else if (landmap[i] == '_') {
    }

    landHeight[i + 1] = currentHeight;
  }

  //    //display i & landHeight[]
  //    for(int i=0; i<N+1; i++) {
  //        dout << setw(2) << i << " ";
  //    }
  //    dout << endl;
  //    for(int i=0; i<N+1; i++) {
  //        dout << setw(2) << landHeight[i] << " ";
  //    }
  //    dout << endl;

  //?????§????°?????±???????
  int min = *min_element(landHeight, landHeight + N + 1);
  int max = *max_element(landHeight, landHeight + N + 1);

  //    dout << "min = " << min << " : max = " << max << endl;

  // print cross-section diagram ????§?Ver.
  dout << "xxx";
  for (int i = 0; i < N + 1; i++) {
    dout << setw(2) << i;
  }
  dout << endl;

  for (int j = max + 1; j >= min; j--) {
    dout << setw(3) << j << " ";
    for (int i = 0; i < N + 1; i++) {
      if (landHeight[i] == j && landmap[i] == '\\\\') {
        dout << "\\\\";
      } else if (landHeight[i] + 1 == j && landmap[i] == '/') {
        dout << "/";
      } else if (landHeight[i] + 1 == j && landmap[i] == '_') {
        dout << "_";
      } else {
        dout << "O";
      }
      dout << " ";
    }
    dout << endl;
  }

  dout << "xxx";
  for (int i = 0; i < N + 1; i++) {
    dout << setw(2) << i;
  }
  dout << endl;

  //    //print cross-section diagram ??¨?§?Ver.
  //    for(int j=max+1; j>=min; j--) {
  //        dout << setw(3) << j << " ";
  //        for(int i=0; i<N+1; i++) {
  //            if(landHeight[i]==j && landmap[i]=='\\\\') {
  //                dout << "???";
  //            }
  //            else if(landHeight[i]+1==j && landmap[i]=='/') {
  //                dout << "???";
  //            }
  //            else if(landHeight[i]+1==j && landmap[i]=='_') {
  //                dout << "???";
  //            }
  //            else {
  //                dout << "??£";
  //            }
  //            //            dout << " ";
  //        }
  //        dout << endl;
  //    }
  //
  //    dout << "xxx";
  //    for(int i=0; i<N+1; i++) {
  //        dout << setw(2) << i;
  //    }
  //    dout << endl;

  return 0;
}