uva10617 Again Palindrome(DP)
#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; }