H.cpp 516 B

123456789101112131415161718192021222324
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. //问题 H: 序列的深度 用一个变量x记住当前所在位置的深度,维护这个变量x的曾经最大值即可
  4. int main() {
  5. char s[100000];
  6. while (cin >> s) {
  7. int x = 0;
  8. int Max = 0;
  9. for (size_t i = 0, n = strlen(s); i < n; i++) {
  10. if (s[i] == '(') {
  11. ++x;
  12. if (x > Max)Max = x;
  13. } else if (s[i] == ')')--x;
  14. }
  15. cout << Max << endl;
  16. }
  17. return 0;
  18. }