您的位置:首页 > 其它

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.

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

2
6
456666
9
466668999

SampleOutput

111456
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;



#include<iostream>
#include<cstring>
usingnamespacestd;
intf[100],g[100],ans[100];
intt,n,k,sum;
intmain(){
cin>>t;
while(t--){
cin>>n;
memset(g,0,sizeofg);//数组g用来记录使用过的左括号
for(inti=1;i<=n;i++){
cin>>f[i];
if(f[i]>f[i-1]){//比前面的数大
ans[i]=1;//ans[i]赋值为一
g[f[i]-ans[i]]=1;//标记使用过的左括号
}
else{
intk=ans[i-1]+1;
while(g[f[i]-k])k++;//找到没有使用过的左括号
g[f[i]-k]=1;
ans[i]=k;
}
}
cout<<ans[1];
for(inti=2;i<=n;i++)
cout<<''<<ans[i];
cout<<endl;
}
return0;
}


ViewCode


http://www.cnblogs.com/keam37/keam所有转载请注明出处
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐
章节导航