loj 1025(记忆化搜索)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25902

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 typedef long long ll;
 7 #define FILL(a,b) memset(a,b,sizeof(a))
 8 
 9 ll dp[77][77];
10 int n;
11 char str[77];
12 
13 ll dfs(int l,int r)
14 {
15     if(dp[l][r]!=-1)return dp[l][r];
16     if(l>r)return dp[l][r]=0;
17     if(l==r)return dp[l][r]=1;
18     dp[l][r]=0;
19     if(str[l]==str[r])dp[l][r]+=dfs(l+1,r-1)+1;
20     dp[l][r]+=dfs(l+1,r)+dfs(l,r-1)-dfs(l+1,r-1);
21     return dp[l][r];
22 }
23 
24 
25 int main()
26 {
27     int _case,t=1;
28     scanf("%d",&_case);
29     while(_case--){
30         scanf("%s",str);
31         n=strlen(str);
32         FILL(dp,-1);
33         printf("Case %d: %lld\n",t++,dfs(0,n-1));
34     }
35     return 0;
36 }
View Code

 

posted @ 2013-10-05 15:59  ihge2k  阅读(197)  评论(0编辑  收藏  举报