Light oj 1110 - An Easy LCS(LCS)

1110 - An Easy LCS
Time Limit: 2 second(s)Memory Limit: 32 MB

LCS means 'Longest Common Subsequence' that means two non-empty strings are given; you have to find the Longest Common Subsequence between them. Since there can be many solutions, you have to print the one which is the lexicographically smallest. Lexicographical order means dictionary order. For example, 'abc' comes before 'abd' but 'aaz' comes before 'abc'.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a blank line. The next two lines will contain two strings of length 1 to 100. The strings contain lowercase English characters only.

Output

For each case, print the case number and the lexicographically smallest LCS. If the LCS length is 0 then just print ':('.

Sample Input

Output for Sample Input

3

 

ab

ba

 

zxcvbn

hjgasbznxbzmx

 

you

kjhs

Case 1: a

Case 2: zxb

Case 3: :(

 


PROBLEM SETTER: JANE ALAM JAN

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define bug printf("hihi\n")

#define eps 1e-12

typedef __int64 ll;

using namespace std;
#define N 105

int dp[N][N];
char f[N][N][N];
char a[N],b[N];
int lena,lenb;

void DP()
{
    int i,j;
    memset(dp,0,sizeof(dp));
    for(i=1;i<=lena;i++)
        for(j=1;j<=lenb;j++)
    {
        if(a[i]==b[j])
        {
            dp[i][j]=dp[i-1][j-1]+1;
            int k;
           strcpy(f[i][j],f[i-1][j-1]);
            f[i][j][dp[i-1][j-1]]=a[i];
        }
        else
            if(dp[i-1][j]>dp[i][j-1])
        {
             dp[i][j]=dp[i-1][j];
             strcpy(f[i][j],f[i-1][j]);
        }
        else
            if(dp[i-1][j]<dp[i][j-1])
        {
            dp[i][j]=dp[i][j-1];
            strcpy(f[i][j],f[i][j-1]);
        }
        else
           if(dp[i-1][j]==dp[i][j-1])
           {
               dp[i][j]=dp[i-1][j];
                if(strcmp(f[i-1][j],f[i][j-1])<0)
                      strcpy(f[i][j],f[i-1][j]);
                else
                    strcpy(f[i][j],f[i][j-1]);
           }
    }
}

int main()
{
    int i,j,t,ca=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s%s",a+1,b+1);
        lena=strlen(a+1);
        lenb=strlen(b+1);
        memset(f,0,sizeof(f));
        DP();
        printf("Case %d: ",++ca);
        if(dp[lena][lenb]==0)
            printf(":(\n");
        else
            printf("%s\n",f[lena][lenb]);
    }
    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值