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: :(

解题思路: 用LCS的模板 加上记录路径就可以了。

但在做这题的过程中提交一直RE, 本地运行没问题。 仔细找之后才发现自己是从i=0,j=0开始遍历的,出现dp[i-1][j-1]的情况就RE,但是dev 不爆:(

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;

const int N = 110;
int dp[N][N];
char str1[N],str2[N],ans[N][N][N];

int main(){

    int T;
    scanf("%d",&T);
    for(int t=1;t<=T;t++){

        memset(dp,0,sizeof(dp));
        memset(ans,0,sizeof(ans));

        scanf("%s %s",str1+1,str2+1);

        int len1 = strlen(str1+1),len2 = strlen(str2+1);
        for(int i=1;i<=len1;i++){
            for(int j=1;j<=len2;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{
                    dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
                    if(dp[i-1][j]<dp[i][j-1]){
                        strcpy(ans[i][j],ans[i][j-1]);
                    }else if(dp[i-1][j]>dp[i][j-1]){
                        strcpy(ans[i][j],ans[i-1][j]);
                    }else{
                        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]);
                        }
                    }
                }
            }
        }
        if(strlen(ans[len1][len2])==0)printf("Case %d: :(\n",t);// strcpy(ans[len1-1][len2-1],":(\0");
        else printf("Case %d: %s\n",t,ans[len1][len2]);

    }

} 
时间: 2024-08-24 12:47:38

lightoj-1110 - An Easy LCS (LCS+路径记录)的相关文章

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.

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

HDU1385 Minimum Transport Cost 【Floyd】+【路径记录】

Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7496    Accepted Submission(s): 1918 Problem Description These are N cities in Spring country. Between each pair of cities

hdu 1258 Sum It Up (dfs+路径记录)

Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3953    Accepted Submission(s): 2032 Problem Description Given a specified total t and a list of n integers, find all distinct sums usi

Charlie&#39;s Change_完全背包&amp;&amp;路径记录

Description Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task. Your program will be given

POJ1734 Sightseeing trip 【Floyd】+【最小环】+【路径记录】

Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4830   Accepted: 1857   Special Judge Description There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attra

HDU 1104 Remainder(BFS路径记录+数论)

Remainder Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4337    Accepted Submission(s): 1095 Problem Description Coco is a clever boy, who is good at mathematics. However, he is puzzled by a d

HDU - 1160 FatMouse&#39;s Speed(dp+路径记录)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 题意:给定x组老鼠的重量(W)和速度(S).求使得   W[m[1]] < W[m[2]] < ... < W[m[n]]       S[m[1]] > S[m[2]] > ... > S[m[n]] 的最长序列长度和路径 题解:排序一下,然后LIS,路径记录一下,输出就可以了 1 #include <iostream> 2 #include <a

台州 OJ 2537 Charlie&#39;s Change 多重背包 二进制优化 路径记录

描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task. Your program will be given numbers