POJ 1068,题目链接http://poj.org/problem?id=1068
对于给出给出的原括号串S,对应两种数字密码串P、W:
S (((()()()))) P-sequence 4 5 6666 (Pi表示第i个右括号前面有多少个左括号) W-sequence 1 1 1456 (Wi表示第i个右括号对应它前面的第几个左括号)
要求给出P串,求W。
1. 模拟类题型。将输入的P串先装换为S串,再由S串得到W串。
2. 左右括号可以用true和false表示。
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 |
//356K 0MS #include <cstdio> #define LEFT true #define RIGHT false bool
s_data[40]; //最多20个括号 int main() { int
caseNum, oneCount; int
temp, last; scanf ( "%d" , &caseNum); do { scanf ( "%d" , &oneCount); int
S_Len = 0; //parenthesesNum * 2 //1. get P-string , and convert to S scanf ( "%d" , &temp); //P - first for
( int i=0; i<temp; ++i) s_data[S_Len++] = LEFT; s_data[S_Len++] = RIGHT; //P second -- count for
( int i=1; i<oneCount; ++i) { last = temp; scanf ( "%d" , &temp); for
( int idx=0; idx<temp-last; ++idx) s_data[S_Len++] = LEFT; s_data[S_Len++] = RIGHT; } //2. convet S to W-string int
pPos = 0; for
( int idx=0; idx < S_Len; ++idx) { if
(s_data[idx] == RIGHT) { int
val=1, ret=1; pPos = idx; while (pPos-- > 0) { if
(s_data[pPos] == RIGHT){ ++val; ++ret; } else
{ --val; if
(val == 0){ //匹配成功 printf ( "%d " , ret); break ; } } } } } printf ( "\n" ); } while
(--caseNum); return
0; } |
poj1068解题报告(模拟类),布布扣,bubuko.com
原文地址:http://www.cnblogs.com/songcf/p/3763653.html