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; }
相关文章推荐
- UVaLive ( LA ) 3516 - Exploring Pyramids (DP 递推)
- LA 3516 Exploring Pyramids (递推)
- LA 3516 区间dp
- zoj 3747 Attack on Titans 带限制条件的计数递推dp
- ZOJ 3141 Arnie's Dog Biscuits【dp递推】
- ZOJ 3872-Beauty of Array(递推||DP)
- 【dp】递推 zoj3747
- 【dp】递推 zoj3747
- LA 3516 - Exploring Pyramids (记忆化搜索dp)
- LA 3516 - Exploring Pyramids (记忆化搜索dp)
- 简单dp之递推(2)--ZOJ 3747
- dp递推 zoj 3747 Attack on Titans
- UVA 10328 , ZOJ 3747 (DP 递推)
- LA3516——dp,记忆化搜索
- LA 3516 (计数 DP) Exploring Pyramids
- POJ 2795 (LA 3516) Exploring Pyramids 解题报告(递推)
- ZOJ 3256 Tour in the Castle(插头DP-按行递推—矩阵)
- zoj 3747 Attack on Titans 递推 计数dp
- LA 4731 蜂窝网络(简单dp+贪心)
- zoj1163 The Staircases (简单dp)