#include #include #include #include #include int main() { // store number of steps taken int num_steps; std::cin >> num_steps; std::cin.ignore(std::numeric_limits::max(), '\n'); int num_valleys = 0; int position = 0; char step_direction; // determine number of valleys for (int i = 0; i < num_steps; ++i) { std::cin >> step_direction; // keep track of "downs" if (step_direction == 'D') { if (position - 1 == -1) { ++num_valleys; --position; } else { --position; } } else if (step_direction == 'U') { ++position; } } // print number of valleys std::cout << num_valleys; return 0; }