POJ2264 HDU1503 Advanced Fruits【LCS】

题目链接:

http://poj.org/problem?id=2264

http://acm.hdu.edu.cn/showproblem.php?pid=1503

题目大意:

两种水果可以杂交出一种新的水果,现在要给新水果起名字,起名的规则是:

这个名字要包含之前两种水果的名字的字母,要按原本字符串中字符的相对顺序。并且这个

名字要尽可能的短。

思路:

先求出两种水果名字s1和s2最长公共子序列的长度,并且用pre[i][j]标记下dp[i][j]的上一个状态,

来得到每个字符在新的字符串中的状态。然后从(len1,len2)回溯输出新的字符串序列。

pre[i][j] == 0表示为上一个字符为公共子串,将s1[i-1]或s2[j-1]输出一次,pre[i][j]
== 1,表示

上一个字符是i-1,将s1[i-1]输出一次,pre[i][j] == 2表示上一个字符是j-1,将s2[j-1]输出一次。

AC代码:

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

int dp[110][110],pre[110][110];
char s1[110],s2[110];

void Print(int i,int j) //回溯输出新的字符串序列
{
    if(i == 0 && j == 0)
        return ;
    if(pre[i][j] == 0)
    {
        Print(i-1,j-1);
        printf("%c",s1[i-1]);
    }
    else if(pre[i][j] == 1)
    {
        Print(i-1,j);
        printf("%c",s1[i-1]);
    }
    else
    {
        Print(i,j-1);
        printf("%c",s2[j-1]);
    }
}

int main()
{
    while(cin >> s1 >> s2)
    {
        memset(dp,0,sizeof(dp));
        int len1 = strlen(s1);
        int len2 = strlen(s2);

        for(int i = 0; i <= len1; ++i)
            pre[i][0] = 1;
        for(int i = 0; i <= len2; ++i)
            pre[0][i] = 2;
        //得到最长公共子序列,并标记dp[i][j]的上一个状态,用来回溯寻找路径
        for(int i = 1; i <= len1; ++i)
        {
            for(int j = 1; j <= len2; ++j)
            {
                if(s1[i-1] == s2[j-1])
                {
                    dp[i][j] = dp[i-1][j-1] + 1;
                    pre[i][j] = 0;
                }
                else if(dp[i-1][j] >= dp[i][j-1])
                {
                    dp[i][j] = dp[i-1][j];
                    pre[i][j] = 1;
                }
                else
                {
                    dp[i][j] = dp[i][j-1];
                    pre[i][j] = 2;
                }
            }
        }
        Print(len1,len2);
        printf("\n");
    }

    return 0;
}
时间: 2024-11-10 17:33:01

POJ2264 HDU1503 Advanced Fruits【LCS】的相关文章

HDU1503:Advanced Fruits 【LCS】

Advanced Fruits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 5   Accepted Submission(s) : 2 Special Judge Problem Description The company "21st Century Fruits" has specialized in creating

hdoj-1503-Advanced Fruits【LCS】

Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2087 Accepted Submission(s): 1072 Special Judge Problem Description The company "21st Century Fruits" has specialized in creati

HDU 1503 Advanced Fruits (LCS,DP)

题意:给你两字符串s1,s2,用最短的字符串表示他们(公共字串输出一次). Sample Input apple peach ananas banana pear peach Sample Output appleach bananas pearch dp[i][j] : 第一个字符串的前 i 个 ,和第二个字符串的前 j 个最短组合的长度 . pre[i][j] : 第一个字符串的第 i 个 ,和第二个字符串的第 j 个字符的状态. #include<cstdio> #include<

HDU 1503 Advanced Fruits(LCS+记录路径)

http://acm.hdu.edu.cn/showproblem.php?pid=1503 题意: 给出两个串,现在要确定一个尽量短的串,使得该串的子串包含了题目所给的两个串. 思路: 这道题目就是要我们求LCS,记录好路径就好. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<sstream> 6 #inc

hdoj 1159 Common Subsequence【LCS】【DP】

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 28494    Accepted Submission(s): 12735 Problem Description A subsequence of a given sequence is the given sequence with some e

HDU 1513 Palindrome【LCS】

题意:给出一个字符串s,问至少加入多少个字母让它变成回文串 解题思路:求出该字符串与该字符串翻转后的最长公共子序列的长度,再用该字符串的长度减去最长公共子序列的长度即为所求 反思:因为题目所给的n的范围为3<=n<=5000,所以dp[][]数组如果开到dp[5005][5005],会超内存,此时应该就用滚动数组来优化 滚动数组的详细介绍http://blog.csdn.net/niushuai666/article/details/6677982 Palindrome Time Limit:

hdoj 1513 Palindrome 【LCS】+【滚动数组】

Palindrome Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3265    Accepted Submission(s): 1130 Problem Description A palindrome is a symmetrical string, that is, a string read identically from

HDU1069_Monkey and Banana【LCS】

Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7823    Accepted Submission(s): 4033 Problem Description A group of researchers are designing an experiment to test the IQ of a

POJ1458 &amp;&amp; HDOJ1159 Common Subsequence【LCS】

题目链接(POJ) :http://poj.org/problem?id=1458 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 40156   Accepted: 16162 Description A subsequence of a given sequence