Hacker Rank Common Child

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <cmath>
#include <cctype>
#include <sstream>
#include <set>
#include <list>
#include <stack>
#include<utility>
#include <queue>
#include <algorithm>
#define sf scanf
#define pf printf
#define sfint(a,b) scanf("%d %d",&a,&b)
#define sfl(a,b) scanf("%ld %ld",&a,&b)
#define sfll(a,b) scanf("%lld %lld",&a,&b)
#define sfd(a,b) scanf("%lf %lf",&a,&b)
#define sff(a,b) scanf("%f %f",&a,&b)
#define lp1(i,n) for(i=0;i<n;i++)
#define lp2(i,n) for(i=1;i<=n;i++)
#define mem(c,v) memset(c,v,sizeof(c))
#define cp(a) cout<<" "<<a<<" "
#define nl puts("")
#define sq(x) ((x)*(x))
#define all(x) x.begin(),x.end()
#define reall(x) x.rbegin(),x.rend()
#define sz size()
#define gc getchar()
#define pb push_back


#define mx7 20000100
#define mx6 1500000
#define mx5 100005
#define mx4 1000100
#define inf 1<<30                                           //infinity value
#define eps 1e-9
#define mx (65540)
#define mod 1000000007
#define pi acos(-1.0)

#define white 0
#define gray 1
#define black -1
#define nil -2

using namespace std;


typedef long long LL;
typedef long L;
typedef unsigned long long ull;
typedef unsigned long ul;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef vector<int>vi;
typedef vector<long long> vll;
typedef vector<long>vl;
typedef vector<char>vch;
typedef vector<string>vs;
typedef map<int,int>mpii;
typedef map<int,bool>mpbi;
typedef map<char,int>mpci;
typedef map<char,bool>mpbc;
typedef map<string,int>mpsi;
typedef map<long long,long long>mpll;

/// template

template<class T> T gcd(T a, T b ) {return b<=0?a:gcd(b,a%b);}
template<class T> T large(T a, T b ) {return a>b?a:b;}
template<class T> T small(T a, T b ) {return a<b?a:b;}
template<class T> T diffrnce(T a, T b) {return a-b<0?b-a:a-b;}

string x,y;
int b[5005][5005],c[5005][5005];
int m,n;

void print_LCS(int i, int j)
{
    if(i==0 or j==0)
    {
        return;
    }

    if(b[i][j] == white)
    {
        print_LCS(i-1,j-1);
        cout << x[i-1];
    }

    else if(b[i][j] == gray)
    {
        print_LCS(i-1,j);
    }

    else
    {
        print_LCS(i,j-1);
    }
}

void LCS()
{
    mem(b,0);
    mem(c,0);

    for(int i=1;i<=m;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(x[i-1] == y[j-1])
            {
                c[i][j] = c[i-1][j-1]+1;
                b[i][j] = white;
            }

            else if(c[i-1][j] >= c[i][j-1])
            {
                c[i][j] = c[i-1][j] ;
                b[i][j] = gray;
            }

            else
            {
                c[i][j] = c[i][j-1];
                b[i][j] = black;
            }
        }
    }

//    for(int i=0;i<=m;i++)
//    {
//        for(int j=0;j<=n;j++)
//        {
//            cout << c[i][j] << " ";
//        }
//
//        nl;
//    }

    //print_LCS(m,n);
}

int main()
{
    while(cin >> x >> y)
    {
        m=x.size();
        n=y.size(); //cout << m << " " << n ; nl;

        LCS();

        cout << c[m][n] << "\n";

        nl;
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LeetCode Palindrome Number

Uva 10650 - Determinate Prime