/** * * * * * */ #include using namespace std; int main() { int n; // number of steps int e; // track elevation char tmp; // tmp var to hold "D" or "U"; will be translated to 0 or 1 // counting variables int mountains = 0; // number of mountains int valleys = 0; // number of valleys int c; // counter of steps do { cin >> n; } while (n < 2 || n > 1000000); char steps[n]; // array to track steps as up or down for (int i = 0; i < n; i++) { // do // { cin >> steps[i]; // } // while (tmp != 'U' || tmp != 'D'); } // track the hike e = 0; c = 0; int stepsM = 0; int stepsV = 0; for (int i = 0; i < n+1; i++) { // move up or down if (steps[i] == 'U') e += 1; else if (steps[i] == 'D') e -= 1; if (e <= -1) { stepsV = stepsV + 1; } // add 1 to valleys if back at sea level if (e == 0 && stepsV >= 1) { valleys = valleys + 1; stepsV = 0; } } cout << valleys << endl; // for (int i = 0; i < n; i++) // { // if (e == 0) // cout << "_"; // if (steps[i] == 'U') // cout << "/"; // if (steps[i] == 'D') // cout << "\\"; // // move up or down // if (steps[i] == 'U') // e += 1; // else if (steps[i] == 'D') // e -= 1; // } // cout << endl; return 0; }