LightOJ 1110 An Easy LCS LCS路径输出

点击打开链接题目链接

1110 - An Easy LCS

PDF (English) Statistics Forum
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: :(

开一个三维的ans数组存储每一次的ans

#include<cstdio>
#include<cstring>
int dp[110][110];
char str1[110],str2[110],ans[110][110][110];
int main()
{
    int t,i,j,k;
    int l1,l2;
    while(scanf("%d",&t)!=EOF)
    {
        for(k=1;k<=t;k++)
        {
            scanf("%s",str1+1);
            scanf("%s",str2+1);
            l1=strlen(str1+1);
            l2=strlen(str2+1);
            memset(dp,0,sizeof(dp));
            memset(ans,0,sizeof(ans));
            for(i=1;i<=l1;i++)
            {
                for(j=1;j<=l2;j++)
                {
                    if(str1[i]==str2[j])
                    {
                        dp[i][j]=dp[i-1][j-1]+1;
                        strcpy(ans[i][j],ans[i-1][j-1]);
                        ans[i][j][dp[i-1][j-1]]=str1[i];
                        ans[i][j][dp[i][j]]='\0';
                    }
                    else if(dp[i-1][j]>dp[i][j-1])
                    {
                        strcpy(ans[i][j],ans[i-1][j]);
                        dp[i][j]=dp[i-1][j];
                    }
                    else if(dp[i-1][j]<dp[i][j-1])
                    {
                        strcpy(ans[i][j],ans[i][j-1]);
                        dp[i][j]=dp[i][j-1];
                    }
                    else
                    {
                        dp[i][j]=dp[i][j-1];
                        if(strcmp(ans[i][j-1],ans[i-1][j])>0)
                        {
                            strcpy(ans[i][j],ans[i-1][j]);
                        }
                        else
                        {
                            strcpy(ans[i][j],ans[i][j-1]);
                        }
                    }
                }
            }
            printf("Case %d: ",k);
            if(dp[l1][l2]==0)
            {
                puts(":(");
            }
            else
            {
                puts(ans[l1][l2]);
            }
        }
    }
    return 0;
}

LightOJ 1110 An Easy LCS LCS路径输出

时间: 2024-10-20 20:20:01

LightOJ 1110 An Easy LCS LCS路径输出的相关文章

lightoj 1110 - An Easy LCS 最长公共子序列+string

1110 - An Easy LCS PDF (English) Statistics Forum 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

lightoj-1110 - An Easy LCS (LCS+路径记录)

1110 - An Easy LCS PDF (English) Statistics ForumTime Limit: 2 second(s) Memory Limit: 32 MBLCS means 'Longest Common Subsequence' that means two non-empty strings are given; you have to find the Longest Common Subsequence between them. Since there c

poj 2250 Compromise dp lcs 路径输出

点击打开链接题目链接 Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6520   Accepted: 2922   Special Judge Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria

POJ 1041 John&#39;s trip 无向图的【欧拉回路】路径输出

欧拉回路第一题TVT 本题的一个小技巧在于: [建立一个存放点与边关系的邻接矩阵] 1.先判断是否存在欧拉路径 无向图: 欧拉回路:连通 + 所有定点的度为偶数 欧拉路径:连通 + 除源点和终点外都为偶数 有向图: 欧拉回路:连通 + 所有点的入度 == 出度 欧拉路径:连通 + 源点 出度-入度=1 && 终点 入度 - 出度 = 1 && 其余点 入度 == 出度: 2.求欧拉路径 : step 1:选取起点(如果是点的度数全为偶数任意点为S如果有两个点的度数位奇数取一

HDU 1026 Ignatius and the Princess I(BFS+路径输出)

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19800    Accepted Submission(s): 6452Special Judge Problem Description The Princess has been abducted by the BEelzebub

URAL 1183 Brackets Sequence DP 路径输出

题意:长度小于100的字符串s只由四种字符"()[]"组成,求以该串为子串的最短的合法串.合法串递归定义为: (1)空串合法 (2)如果S合法,则(S).[S]合法 (3)如果A.B合法,则AB合法 思路: 设dp[i][j]为s(i,j)变为合法串后,合法串的长度或需要添加的字符的个数,状态转移: (1)如果s[i]和s[j]匹配,dp[i,j]=dp[i+1,j-1]. (2)如果不匹配,划分s(i,j)为s(i,k)和s(k+1,j),划分后dp[i,j]=dp[i,k]+dp[

poj3414 Pots(BFS+路径输出)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=3414 此题和poj1606一样 :http://blog.csdn.net/u012860063/article/details/37772275 Description You are given two pots, having the volume of A and B liters respectively.

poj1606 Jugs(BFS+路径输出)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1606 此题和poj3414一样:http://blog.csdn.net/u012860063/article/details/37768979 Description In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were co

(review)zoj1276 区间dp+路径输出

[题解]:经典的区间dp,并且记录下了dp的path 因为是递归得到的path,所以递归压栈按从里到外的顺序得到path就可以了 输出嵌套括号部分很好的考察了对栈的理解,和递归执行的顺序. 注意题目输出中有的地方有空格 1 //zoj1276 路径输出用到了栈的思想,比较考验思维 2 #include<iostream> 3 #include<string.h> 4 #include<stdio.h> 5 #define maxn 13 6 using namespac