hdu 1503 最大公共子序列+输出路径

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;

int max(int a,int b)
{
    return a>a?a:b;
}
char str1[110],str2[110];
int mark[110][110];
int deal(int i,int j)
{

    if(i==0&&j==0) return 0;
    if(mark[i][j]==0)
    {
        deal(i-1,j-1);
        printf("%c",str1[i-1]);
    }
    else if(mark[i][j]==1)
    {
        deal(i-1,j);
        printf("%c",str1[i-1]);
    }
    else if(mark[i][j]==2)
    {
        deal(i,j-1);
        printf("%c",str2[j-1]);
    }
    return 0;
}
int main()
{

    int i,j;
    int dp[110][110];
    while(~scanf("%s%s",str1,str2))
    {
        int len1=strlen(str1);
        int len2=strlen(str2);
        memset(dp,0,sizeof(dp));
         for(i = 0;i<=len1;i++)
        mark[i][0] = 1;
        for(i = 0;i<=len2;i++)
        mark[0][i] = 2;
        for(i=1;i<=len1;i++)
        {
            for(j=1;j<=len2;j++)
            {
                if(str1[i-1]==str2[j-1])
                {
                    dp[i][j]=dp[i-1][j-1]+1;
                    mark[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])
                    {
                        dp[i][j]=dp[i-1][j];
                        mark[i][j]=1;
                    }
                    else
                    {
                        mark[i][j]=2;
                        dp[i][j]=dp[i][j-1];
                    }
                }
            }
        }
        //printf("****\n");
        deal(len1,len2);
        printf("\n");
    }
    return 0;
}
时间: 2024-11-08 17:07:41

hdu 1503 最大公共子序列+输出路径的相关文章

Ignatius and the Princess I (hdu 1026 优先队列+bfs+输出路径)

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

hdu 1513 最大公共子序列

很明显  将串反转与原串求LCS   然后用总长减即可. #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int max(int a,int b) { return a>b?a:b; } int main() { int len,i,j; char str[5010],str1[5010]; int dp[3][5010]; while(~scanf(&quo

hdu 1503 LCS输出路径【dp】

hdu 1503 不知道最后怎么输出,因为公共部分只输出一次.有人说回溯输出,感觉好巧妙!其实就是下图,输出的就是那条灰色的路径,但是初始时边界一定要初始化一下,因为最第一列只能向上走,第一行只能向左走.我用1表示向上走,2向左上方走,3向左走. 刚开始输入字符串时有两种方法,直接输入:或从地址后一位输入,即此时数组起始编号为1.直接输入时,dp[i][j]表示的是以s1[i-1],s2[j-1]为结尾LCS,另一种则就是表示以s1[i],s2[j]为结尾的LCS.两者在路径输出时有些差别,以前

HDU 1503【LCS】(字符串合并输出)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503 题目大意: 给两个字符串,组成一个长度尽可能小的字符串,它包含上述两个字符串,且原字符串中的字符在该串中的相对位置不变. Sample Input apple peach ananas banana pear peach Sample Output appleach bananas pearch 解题思路:要结合样例来理解题意,本题主要难在如何输出题目要求字符串,这就需要我们仔细研究样例,去发

hdu 1160 FatMouse&#39;s Speed(最长不下降子序列+输出路径)

题意: FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the s

hdu Minimum Transport Cost(按字典序输出路径)

 摘自:Think In Java 从技术角度说,OOP(面向对象程序设计)只是涉及抽象的数据类型.继承以及多形性,但另一些问题也可能显得非常重要.本节将就这些问题进行探讨.最重要的问题之一是对象的创建及破坏方式.对象需要的数据位于哪儿,如何控制对象的"存在时间"呢?针对这个问题,解决的方案是各异其趣的.C++认为程序的执行效率是最重要的一个问题,所以它允许程序员作出选择.为获得最快的运行速度,存储以及存在时间可在编写程序时决定,只需将对象放置在堆栈(有时也叫作自动或定域变量)或者静态

HDU 1159 Common Subsequence 最大公共子序列

Problem Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, ..., xm> another sequence Z = <z1, z2, ..., zk> is a subsequence of X if there exists a stri

hdu 1385 Floyd 输出路径

Floyd 输出路径 Sample Input50 3 22 -1 43 0 5 -1 -122 5 0 9 20-1 -1 9 0 44 -1 20 4 05 17 8 3 1 //收费1 3 //起点 终点3 52 4-1 -10 Sample OutputFrom 1 to 3 :Path: 1-->5-->4-->3Total cost : 21 From 3 to 5 :Path: 3-->4-->5Total cost : 16 From 2 to 4 :Path

hdu 1026 Ignatius and the Princess I(bfs搜索+输出路径)

题目来源:hdu-1026 Ignatius and the Princess I Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14677 Accepted Submission(s): 4653 Special Judge Problem Description The Princ