-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathZOJ1016(AC).cpp
111 lines (101 loc) · 1.23 KB
/
ZOJ1016(AC).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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <stdio.h>
int findmatch(char s[], int a, int n)
{
int stack;
int i;
if(s[a] != ')')
{
return -1;
}
stack = 0;
for(i = a; i >= 0; i--)
{
if(s[i] == ')')
{
stack++;
}
else if(s[i] == '(')
{
stack--;
if(stack == 0)
{
return i;
}
}
}
}
int main()
{
int i;
int j;
int k;
int match;
int cw;
int l;
int n;
int t;
int p[100];
int w[100];
char s[201];
scanf("%d", &t);
for(i = 0; i < t; i++)
{
scanf("%d", &n);
for(j = 0; j < n; j++)
{
scanf("%d", &p[j]);
w[j] = 0;
}
l = 0;
for(j = 0; j < n; j++)
{
if(j == 0)
{
for(k = 0; k < p[j]; k++)
{
s[l + k] = '(';
}
s[l + k] = ')';
l = l + p[j] + 1;
}
else
{
for(k = 0; k < p[j] - p[j - 1]; k++)
{
s[l + k] = '(';
}
s[l + k] = ')';
l = l + p[j] - p[j - 1] + 1;
}
}
s[2 * n] = 0;
cw = 0;
for(j = 0; j < 2 * n; j++)
{
if(s[j] == ')')
{
match = findmatch(s, j, 2 * n);
for(k = match; k <= j; k++)
{
if(s[k] == ')')
{
w[cw]++;
}
}
cw++;
}
}
for(j = 0; j < n; j++)
{
if(j == n - 1)
{
printf("%d\n", w[j]);
}
else if(j < n - 1)
{
printf("%d ", w[j]);
}
}
}
return 0;
}