Showing posts with label DP. Show all posts
Showing posts with label DP. Show all posts

Sunday, March 26, 2017

1033 - Generating Palindromes

Problem Link : http://lightoj.com/volume_showproblem.php?problem=1033

Solution :

#include<bits/stdc++.h>

using namespace std;

int a[102][102];

int main()
{
    string text,pattern;

    int t,l1;
    scanf("%d",&t);

    for(int ca=1; ca<=t; ca++)
    {
        cin>>text;
        pattern=text;
        reverse(pattern.begin(),pattern.end());

        l1=text.size();

        memset(a,0,sizeof(a));

        for(int i=1; i<=l1; i++)
        {
            for(int j=1; j<=l1; j++)
            {
                if(text[i-1]==pattern[j-1]) a[i][j]=a[i-1][j-1]+1;
                else a[i][j]=max(a[i-1][j],a[i][j-1]);
            }
        }

        cout<<"Case "<<ca<<": "<<l1-a[l1][l1]<<endl;

    }



}

Friday, May 20, 2016

674 - Coin Change

Problem linkhttps://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=615

Solution :

#include<bits/stdc++.h>

using namespace std;

int dp[6][7492];

int amnt[]= {50,25,10,5,1};

int coin(int i,int ammount)
{
    if(i>=5)
    {
        if(ammount==0) return 1;
        else return 0;

    }

    int ret1=0,ret2=0;

    if(dp[i][ammount]!=-1) return dp[i][ammount];

    if(ammount-amnt[i]>=0) ret1=coin(i,ammount-amnt[i]);
    ret2=coin(i+1,ammount);

    return dp[i][ammount]=ret1+ret2;

}

int main()
{
    int make;

    memset(dp,-1,sizeof(dp));

    while(scanf("%d",&make)==1)
    {
        cout<<coin(0,make)<<endl;
    }

}

10130 - SuperSale

Problem link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1071

Solution :

#include<bits/stdc++.h>

using namespace std;

long long int dp[1005][35];

long long int weight[1005];
long long int cost[1005];
long long int cap;
long long int node;
long long int ans=0;

long long int knapsack(long long int idx,long long int w)
{
    if(idx==node+1) return 0;

    if(dp[idx][w]!=-1) return dp[idx][w];

    int profit1=0,profit2=0;

    if(w+weight[idx]<=cap) profit1=cost[idx]+knapsack(idx+1,w+weight[idx]);

    profit2=knapsack(idx+1,w);

    dp[idx][w]=max(profit1,profit2);

    return dp[idx][w];
}


int main()
{


    long long int t,i,g,p;
    cin>>t;


    while(t--)
    {
        cin>>node;
        p=node;

        i=1;
        while(p--)
        {
            cin>>cost[i]>>weight[i];
            i++;
        }
        cin>>g;
        while(g--)
        {
            memset(dp,-1,sizeof(dp));
            cin>>cap;
            ans=ans+knapsack(1,0);
        }
        cout<<ans<<endl;
        ans=0;
    }

}

Wednesday, May 18, 2016

1006 - Hex-a-bonacci

Problem link : http://www.lightoj.com/volume_showproblem.php?problem=1006


Solution :

#include<bits/stdc++.h>

using namespace std;

long long int dp[10050];

long long int a, b, c, d, e, f;
long long int fn( long long int n )
{
    if(dp[n]!=-1) return dp[n];
    if( n == 0 ) return a;
    if( n == 1 ) return b;
    if( n == 2 ) return c;
    if( n == 3 ) return d;
    if( n == 4 ) return e;
    if( n == 5 ) return f;
    return dp[n]=(( fn(n-1) + fn(n-2) + fn(n-3) + fn(n-4) + fn(n-5) + fn(n-6) )%10000007);
}
int main()
{

    long long int n, caseno = 0, cases;


    scanf("%lld", &cases);
    while( cases-- )
    {
        memset(dp,-1,sizeof(dp));
        scanf("%lld %lld %lld %lld %lld %lld %lld", &a, &b, &c, &d, &e, &f, &n);

        printf("Case %lld: %lld\n", ++caseno, fn(n)%10000007);
    }
    return 0;

}