-
Notifications
You must be signed in to change notification settings - Fork 0
/
括号问题.cpp
85 lines (82 loc) · 1.74 KB
/
括号问题.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
using namespace std;
int main()
{
int n(0);
while (cin >> n)
{
char *input = new char[n + 1];
cin >> input;
//int head(0), tail(n - 1);//标记头尾
//cout << Score(head, tail, input) << endl;
//如果使用压栈的逆波兰式的思想呢?
//假设input为一个栈
char *stack = new char[n + 1];
char *store = new char[n + 1];
int point(0);
int store_i(0);
for (int i = 0; i < n; ++i)
{
if (input[i] == '(' && input[i + 1] == ')')
{
++i;
stack[point] = 1;
//cout << "?" << stack[point] << "!" << endl;
++point;
}
else//此时不是成对出现的
{
stack[point] = input[i];
++point;
}
}
//for (int i = 0; i < point; ++i)
//cout << "No." << i << ":" << (int)stack[i] << endl;
//cout << endl;
int score(0), mid(0);
int final_point(0);
int top(0);
for (int i = 0; i < point; ++i)
{
if (stack[i] == '(')
{
store[final_point] = '(';
++final_point;
}
else if (stack[i] == 1)
{
store[final_point] = 1;
++final_point;
}
else//此时为括号结尾
{
--final_point;
for (; final_point > 0; --final_point)
{
//for (int i = 0; i <= final_point; ++i)
//cout << (int)store[i] << " ";
//cout << endl;
if (store[final_point - 1] != '(')
{
store[final_point - 1] += store[final_point];
}
else
{
store[final_point] *= 2;
store[final_point - 1] = store[final_point];
break;
}
}
}
//cout << store << endl;
}
--final_point;
for (; final_point > 0; --final_point)
{
store[final_point - 1] += store[final_point];
}
cout << (int)store[0] << endl;
//cout << (int)store[1] << endl;
}
return 0;
}