Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Sunday, September 27, 2015

BVAAN-Balika Vadhu and Alok Nath

Balika Vadhu and Alok Nath

given below c++14 code is for BVAAN spoj or balika vadhu and alok nath spoj.

hint :- 3D Dynamic programming problem .




#include <bits/stdc++.h>
using namespace std;
#define pb push_back
int sol(map<tuple<int ,int ,int > , int > &mp , int i , int j , int k , string &a , string &b){
    if(k == 0)
        return 0;
    if(i == -1 || j == -1)
        return -1;
    auto it = mp.find(make_tuple(i , j , k));
    if(it!=mp.end())
        return it->second;
    int first = -1;
    if(a[i] == b[j]){
        first = sol(mp , i-1 , j-1 , k-1 , a , b);
        if(first != -1)
            first += a[i];
    }
    int sec = -1 , thd = -1;
    sec = sol(mp , i-1 , j , k , a , b);
    thd = sol(mp , i , j-1 , k , a , b);
    mp[make_tuple(i , j , k)] = max(first , max(sec , thd));
    return max(first , max(sec , thd));
}
int main(){

    int t;
    cin>>t;
    while(t--){
        string a , b ;
        int k;
        cin>>a>>b>>k;
        map<tuple<int , int , int > , int > mp;
        int res = sol(mp , a.size() -1 , b.size()-1 , k , a , b);
        if(res == -1)
            cout<<0<<endl;
        else
            cout<<res<<endl;
    }
    return 0;
}

1 comment:

Your comment is valuable to us