POJ1068 Parencodings 解题报告
2014-05-24 00:45
435 查看
Description
LetS=s1s2...s2nbeawell-formedstringofparentheses.Scanbeencodedintwodifferentways:
qByanintegersequenceP=p1p2...pnwherepiisthenumberofleftparenthesesbeforetheithrightparenthesisinS(P-sequence).
qByanintegersequenceW=w1w2...wnwhereforeachrightparenthesis,sayainS,weassociateanintegerwhichisthenumberofrightparenthesescountingfromthematchedleftparenthesisofauptoa.(W-sequence).
Followingisanexampleoftheaboveencodings:
S(((()()())))
P-sequence 456666
W-sequence 111456
WriteaprogramtoconvertP-sequenceofawell-formedstringtotheW-sequenceofthesamestring.
LetS=s1s2...s2nbeawell-formedstringofparentheses.Scanbeencodedintwodifferentways:
qByanintegersequenceP=p1p2...pnwherepiisthenumberofleftparenthesesbeforetheithrightparenthesisinS(P-sequence).
qByanintegersequenceW=w1w2...wnwhereforeachrightparenthesis,sayainS,weassociateanintegerwhichisthenumberofrightparenthesescountingfromthematchedleftparenthesisofauptoa.(W-sequence).
Followingisanexampleoftheaboveencodings:
S(((()()())))
P-sequence 456666
W-sequence 111456
WriteaprogramtoconvertP-sequenceofawell-formedstringtotheW-sequenceofthesamestring.
Input
Thefirstlineoftheinputcontainsasingleintegert(1<=t<=10),thenumberoftestcases,followedbytheinputdataforeachtestcase.Thefirstlineofeachtestcaseisanintegern(1<=n<=20),andthesecondlineistheP-sequenceofawell-formedstring.Itcontainsnpositiveintegers,separatedwithblanks,representingtheP-sequence.
Output
Theoutputfileconsistsofexactlytlinescorrespondingtotestcases.Foreachtestcase,theoutputlineshouldcontainnintegersdescribingtheW-sequenceofthestringcorrespondingtoitsgivenP-sequence.
SampleInput
6
456666
9
466668999
SampleOutput
112451139
分析:
对于给出的序列,明显是一定不会下降的,
用ans[]来记录答案
当A[i]>A[j]时其对应的ans[i]=1;
注意到A[i]-ans[i]其实就是当前右括号对应的左括号的前面有多少个左括号
很明显对每个A[i]-ans[i]都是唯一的,用它来作为匹配过的左括号的编号,这个编号从0开始
所以当A[i]==A[i-1]时令k=ans[i-1]+1;
不断增加k直到A[i]-k的值没有在前面出现过时,ans[i]=k;