zoukankan      html  css  js  c++  java
  • lightoj-1110

    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]] = '';                    
                    }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: :(
    ",t);// strcpy(ans[len1-1][len2-1],":(");
            else printf("Case %d: %s
    ",t,ans[len1][len2]);
            
        }
        
        
    } 
  • 相关阅读:
    《软件需求十步走》阅读笔记一
    专业实训项目需求分析
    2015秋季学期个人阅读计划
    最后期限阅读笔记3
    最后期限阅读笔记2
    最后期限阅读笔记1
    你的灯亮着么阅读笔记3
    DPDK(二):准备知识7---linux虚拟网络设备
    DPDK(二)准备6---PMD
    DPDK(二):准备知识4 --- 无锁FIFO环形队列
  • 原文地址:https://www.cnblogs.com/yuanshixingdan/p/5568826.html
Copyright © 2011-2022 走看看