শুক্রবার, ২ নভেম্বর, ২০১৮

Find the lexicographically smallest LCS (Longest Common Subsequence)

IF the lcs length is 0 then print :(
INPUT:
3
ab
ba

zxcvbn
hjgasbznxbzmx

you
kghs

OUTPUT:
a
zxb
:(

CODE
///...................SUBHASHIS MOLLICK...................///
///.....DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING....///
///.............ISLAMIC UNIVERSITY,BANGLADESH.............///
///....................SESSION-(14-15)....................///
#include<bits/stdc++.h>
using namespace std;
#define sf(a) scanf("%lld",&a)
#define sf2(a,b) scanf("%lld %lld",&a,&b)
#define sf3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define pf(a) printf("%lld",a)
#define pf2(a,b) printf("%lld %lld",a,b)
#define pf3(a,b,c) printf("%lld %lld %lld",a,b,c)
#define nl printf("\n")
#define   timesave              ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
#define pb push_back
#define MPI map<int,int>mp;
#define fr(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
#define frl(i,a,b) for(i=a;i<=b;i++)
/*primes in range 1 - n
1 - 100(1e2) -> 25 pimes
1 - 1000(1e3) -> 168 primes
1 - 10000(1e4) -> 1229 primes
1 - 100000(1e5) -> 9592 primes
1 - 1000000(1e6) -> 78498 primes
1 - 10000000(1e7) -> 664579 primes
large primes ->
104729 1299709 15485863 179424673 2147483647 32416190071 112272535095293 48112959837082048697
*/
//freopen("Input.txt","r",stdin);
//freopen("Output.txt","w",stdout);
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
string a,b;
long cs=1,n,m;
void call()
{
    long dp[n+1][m+1],i,j;
    string s[n+1][m+1];
    for(i=0; i<=n; i++)
    {
        dp[i][0]=0;
        s[i][0]="";
    }
    for(i=0; i<=m; i++)
    {
        dp[0][i]=0;
        s[0][i]="";
    }
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            if(a[i-1]==b[j-1])
            {
                dp[i][j]=1+dp[i-1][j-1];
                s[i][j]=s[i-1][j-1]+a[i-1];
            }
            else if(dp[i-1][j]>dp[i][j-1])
            {
                dp[i][j]=dp[i-1][j];
                s[i][j]=s[i-1][j];
            }
            else if(dp[i][j-1]>dp[i-1][j])
            {
                dp[i][j]=dp[i][j-1];
                s[i][j]=s[i][j-1];
            }
            else
            {
                dp[i][j]=dp[i-1][j];
                s[i][j]=min(s[i-1][j],s[i][j-1]);
            }
        }
    }
    if(dp[n][m]==0)
        cout<<"Case "<<cs++<<": :("<<endl;
    else
        cout<<"Case "<<cs++<<": "<<s[n][m]<<endl;
}
main()
{
    timesave;
    long ts;
    cs=1;
    cin>>ts;
    while(ts--)
    {
        cin>>a>>b;
        n=a.size(),m=b.size();
        call();
    }
}


কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন

Factorization with prime Sieve

vector <int> prime; char sieve[1000009]; int N=1000009; void primeSieve ( ) { sieve[0] = sieve[1] = 1; prime.push_back(2); ...