poj 2795 Exploring Pyramids

Exploring Pyramids
Time Limit: 6000MS Memory Limit: 65536K
Total Submissions: 1971 Accepted: 899
Case Time Limit: 2000MS

Description

Archaeologists have discovered a new set of hidden caves in one of the Egyptian pyramids. The decryption of ancient hieroglyphs on the walls nearby showed that the caves structure is as follows. There are n caves in a pyramid, connected by narrow passages, one of the caves is connected by a passage to the outer world. The system of the passages is organized in such a way, that there is exactly one way to get from outside to each cave along passages. All caves are located in the basement of the pyramid, so we can consider them being located in the same plane. Passages do not intersect. Each cave has its walls colored in one of several various colors. 

The scientists have decided to create a more detailed description of the caves, so they decided to use an exploring robot. The robot they are planning to use has two types of memory - the output tape, which is used for writing down the description of the caves, and the operating memory organized as a stack. 

The robot first enters the cave connected to the outer world along the passage. When it travels along any passage for the first time, it puts its description on the top of its stack. When the robot enters any cave,it prints the color of its walls to its output tape. After that it chooses the left most passage among thosethat it has not yet travelled and goes along it. If there is no such passage, the robot takes the passage description from the top of its stack and travels along it in the reverse direction. The robot's task is over when it returns to the outside of the pyramid. It is easy to see that during its trip the robot visits each cave at least once and travels along each passage exactly once in each direction. 

The scientists have sent the robot to its mission. After it returned they started to study the output tape. What a great disappointment they have had after they have understood that the output tape doesnot describe the cave system uniquely. Now they have a new problem - they want to know how many different cave systems could have produced the output tape they have. Help them to find that out. 

Since the requested number can be quite large, you should output it modulo 1 000 000 000. Please note,that the absolute locations of the caves are not important, but their relative locations are important, sothe caves (c) and (d) on the picture below are considered different. 

Input

The input contains the output tape that the archaeologists have. The output tape is the sequence of colors of caves in order the robot visited them. The colors are denoted by capital letters of the English alphabet. The length of the tape does not exceed 300 characters.

Output

Output one integer number - the number of different cave systems (modulo 1 000 000 000) that could produce the output tape.

Sample Input

ABABABA

Sample Output

5

Hint

Sample input 2 
AB 
Sample output 2 
0

Source



题意:

给出一个多叉树先序遍历的序列,问多叉树的形态有多少种。


思路:

dp[i][j]表示 i-j 能组成的枝干的形态 ,那么

if(s[i]==s[j])  dp[i][j]=dp[i+1][j-1]+∑dp[i][k]*dp[k+1][j-1]   (s[k]==s[i]&&i+1<k<j-1)  因为分出一个分支的访问时序列的长度至少为2(来、回)。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 305
#define MAXN 100005
#define mod 1000000000
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

ll n,m,k,ans,cnt,tot,flag;
ll dp[maxn][maxn];
char s[maxn];

ll dfs(ll x,ll y)
{
//    printf("x:%lld y:%lld\n",x,y);
    if(x>=y) return 1;
    if(dp[x][y]!=-1) return dp[x][y];
    ll i,j,t=0;
    if(s[x]==s[y]&&y>x+1)
    {
        t+=dfs(x+1,y-1);
        for(i=x+2;i<y-1;i++)
        {
            if(s[i]==s[x])
            {
                t+=dfs(x,i)*dfs(i+1,y-1);
                t%=mod;
            }
        }
    }
    dp[x][y]=t;
    return t;
}
int main()
{
    int i,j,t;
    while(~scanf("%s",s+1))
    {
        memset(dp,-1,sizeof(dp));
        n=strlen(s+1);
        ans=dfs(1LL,n);
        printf("%I64d\n",ans);
    }
    return 0;
}
/*
ABABABA
AAAA
AB
*/

 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值