您的位置:首页 > 其它

LA 3516(ZOJ 2641) Exploring Pyramids(递推 DP)

2013-07-18 14:04 781 查看

ExploringPyramids

ArchaeologistshavediscoveredanewsetofhiddencavesinoneoftheEgyptianpyramids.Thedecryptionofancienthieroglyphsonthewallsnearbyshowedthatthecavesstructureisasfollows.Therearen<tex2html_verbatim_mark>cavesinapyramid,connectedbynarrowpassages,oneofthecavesisconnectedbyapassagetotheouterworld.Thesystemofthepassagesisorganizedinsuchaway,thatthereisexactlyonewaytogetfromoutsidetoeachcavealongpassages.Allcavesarelocatedinthebasementofthepyramid,sowecanconsiderthembeinglocatedinthesameplane.Passagesdonotintersect.Eachcavehasitswallscoloredinoneofseveralvariouscolors.

Thescientistshavedecidedtocreateamoredetaileddescriptionofthecaves,sotheydecidedtouseanexploringrobot.Therobottheyareplanningtousehastwotypesofmemory-theoutputtape,whichisusedforwritingdownthedescriptionofthecaves,andtheoperatingmemoryorganizedasastack.

Therobotfirstentersthecaveconnectedtotheouterworldalongthepassage.Whenittravelsalonganypassageforthefirsttime,itputsitsdescriptiononthetopofitsstack.Whentherobotentersanycave,itprintsthecolorofitswallstoitsoutputtape.Afterthatitchoosestheleftmostpassageamongthosethatithasnotyettravelledandgoesalongit.Ifthereisnosuchpassage,therobottakesthepassagedescriptionfromthetopofitsstackandtravelsalongitinthereversedirection.Therobot'staskisoverwhenitreturnstotheoutsideofthepyramid.Itiseasytoseethatduringitstriptherobotvisitseachcaveatleastonceandtravelsalongeachpassageexactlyonceineachdirection.

Thescientistshavesenttherobottoitsmission.Afteritreturnedtheystartedtostudytheoutputtape.Whatagreatdisappointmenttheyhavehadaftertheyhaveunderstoodthattheoutputtapedoesnotdescribethecavesystemuniquely.Nowtheyhaveanewproblem-theywanttoknowhowmanydifferentcavesystemscouldhaveproducedtheoutputtapetheyhave.Helpthemtofindthatout.

Sincetherequestednumbercanbequitelarge,youshouldoutputitmodulo1000000000.Pleasenote,thattheabsolutelocationsofthecavesarenotimportant,buttheirrelativelocationsareimportant,sothecaves(c)and(d)onthepicturebelowareconsidereddifferent.


<tex2html_verbatim_mark>

Input

Theinputfilecontainsseveraltestcases,andeachofthemconsistsofasinglelinewiththeoutputtapethatthearchaeologistshave.Theoutputtapeisthesequenceofcolorsofcavesinordertherobotvisitedthem.ThecolorsaredenotedbycapitallettersoftheEnglishalphabet.Thelengthofthetapedoesnotexceed300characters.

Output

Foreachinputcase,writetotheoutputasinglelinecontainingoneintegernumber-thenumberofdifferentcavesystems(modulo1000000000)thatcouldproducetheoutputtape.

SampleInput

ABABABA
AB

SampleOutput

5
0

题目大意:给出一棵多叉树,每个结点的任意两个子结点都有左右之分。从根结点开始,每次尽量往左走,走不通了就回溯,把遇到的字母顺序记录下来,可以得到一个序列。如图所示的序列均为ABABABA。给定一个序列,问有多少棵树与之对应。

分析:设输入序列为S,d(i,j)为子序列Si,Si+1,...,Sj对应的个数,则边界条件是d(i,i)=1,且Si不等于Sj时d(i,j)=0(因为起点和终点应是同一点)。在其他情况下,设第一个分支在Sk时回到树根(必须有Si=Sk),则这个分支对应的序列是Si+1,...,Sk-1,方案数为d(i+1,k-1),其他分支对应的访问序列为Sk,...,Sj,方案数为d(k,j)。这样,在非边界情况,递推关系为d(i,j)=sum{d(i+1,k-1)*d(k,j)|i+2<=k<=j,Si=Sk=Sj}

代码如下


#include<cstdio>
#include<cstring>
usingnamespacestd;

constintmaxn=300+10;
constintMOD=1000000000;
typedeflonglongLL;

charS[maxn];
intd[maxn][maxn];

intdp(inti,intj){
if(i==j)return1;
if(S[i]!=S[j])return0;
int&ans=d[i][j];
if(ans>=0)returnans;
ans=0;
for(intk=i+2;k<=j;k++)if(S[i]==S[k])
ans=(ans+(LL)dp(i+1,k-1)*(LL)dp(k,j))%MOD;
returnans;
}

intmain(){
while(scanf("%s",S)==1){
memset(d,-1,sizeof(d));
printf("%d\n",dp(0,strlen(S)-1));
}
return0;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: