UVa1362 Exploring Pyramids

 

区间dp,枚举走完第一个子树之后回到根节点的位置。 

 

 1 /*by SilverN*/
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 #include<vector>
 8 using namespace std;
 9 const int mod=1e9;
10 const int mxn=310;
11 int read(){
12     int x=0,f=1;char ch=getchar();
13     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
14     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
15     return x*f;
16 }
17 char s[mxn];
18 int f[mxn][mxn];
19 int main(){
20     int i,j;
21     while(scanf("%s",s+1)!=EOF){
22         int n=strlen(s+1);
23         memset(f,0,sizeof f);
24         for(i=1;i<=n;i++)f[i][i]=1;
25         for(int st=2;st<=n;st++){
26             for(i=1;i<=n;i++){
27                 j=i+st-1;if(j>n)break;
28                 if(s[i]==s[j]){
29                     for(int k=i+2;k<=j;k++){
30                         if(s[k]==s[i]){
31                             f[i][j]=((long long)f[i][j]+((long long)f[i+1][k-1]*f[k][j])%mod)%mod;
32                         }
33                     }
34                 }
35             }
36         }
37         printf("%d\n",f[1][n]);
38     }
39     return 0;
40 }

 

posted @ 2017-01-13 16:22  SilverNebula  阅读(120)  评论(0编辑  收藏  举报
AmazingCounters.com