Contents
  1. 1. 题目大意:

题目大意:

输出有多少个括号是符合的,例如:
()()()
6
(()))
4
()[()
4
括号为:(,),[,]

  • 补:另一个思路是,记录’(‘,’)’,’[‘,’]’的个数,然后从‘(’和‘)’的数目中选择最小的乘2,[和]类似
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <algorithm>
using namespace std;
#define MAXN 150

stack <char> sta;
int dp[MAXN];


int main()
{

char str[150], flag;
while(scanf("%s", str + 1) != EOF){
memset(dp, 0, sizeof(dp));
flag = 0;
if(str[1] == 'e')
break;
int len = strlen(str);
for(int i = 1; i < len; i++){ //dp[0] = 0
if(str[i] == '(' || str[i] == '['){
if(str[i] == '[')
flag++; //'['的数目用flag表示
if(str[i] == '(')
sta.push(str[i]); //栈里只存'('
dp[i] = dp[i - 1];
continue;
}
if(str[i] == ')' && !sta.empty()){
dp[i] = dp[i - 1] + 2;
sta.pop();
continue;
}
if(str[i] == ']' && flag){ //如果有']'
flag--;
dp[i] = dp[i - 1] + 2;
}
else
dp[i] = dp[i - 1];
}
printf("%d\n", dp[len - 1]);
}

return 0;
}
Contents
  1. 1. 题目大意: