Saturday, 14 January 2017

Light OJ 1025 - The Specials Menu

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll arr[100];
ll dp[100][100];
ll l,n;
string s;
ll solve(ll i,ll j)
{
    if(i>j)
        return 0;
    if(i==j)
        return 1;
    if(dp[i][j]!=-1)
        return dp[i][j];
    if(s[i]==s[j])
    {
        dp[i][j]=1+solve(i+1,j)+solve(i,j-1);
    }
    if(s[i]!=s[j])
    {
        dp[i][j]=solve(i+1,j)+solve(i,j-1)-solve(i+1,j-1);
    }
    return dp[i][j];
}
int main()
{
    ll i,j,k,m,n,test;
    cin>>test;
    for(k=1;k<=test;k++)
    {
        cin>>s;
        memset(dp,-1,sizeof(dp));
        l=s.size();
        cout<<"Case "<<k<<": "<<solve(0,l-1)<<endl;
    }
    return 0;
}

No comments:

Post a Comment