uva10617 Again Palindrome(DP)

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=1558

 

#include <iostream>
#include <string>
#include <cstring>
#define ll long long
using namespace std;
int t,l;
ll dp[65][65];
char s[100];
ll DP(int x,int y){
    if(dp[x][y]!=-1) return dp[x][y];
    if(x>y) return 0;
    if(x==y) return dp[x][y]=1;
    ll ret=0;
    for(int i=x;i<=y;i++){
       for(int j=i;j<=y;j++){
          if(s[i]==s[j]){
              ret+=DP(i+1,j-1)+1;
          }
       }
    }
    return dp[x][y]=ret;
}
int main(){
    cin>>t;
    while(t--){
        cin>>s+1;
        l=strlen(s+1);
        memset(dp,-1,sizeof dp);
        cout<<DP(1,l)<<endl;
    }
    return 0;
}
uva10617

 

posted @ 2014-02-09 20:43  wonderzy  阅读(136)  评论(0编辑  收藏  举报