HDU 1159 Common Subsequence --- DP入门之最长公共子序列

题目链接

基础的最长公共子序列

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e3+5;
char c[maxn],d[maxn];
int dp[maxn][maxn];
int main()
{
    while(scanf("%s%s",c,d)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        int n=strlen(c);
        int m=strlen(d);
        for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        if(c[i]==d[j])
        dp[i+1][j+1]=dp[i][j]+1;
        else
        dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
        printf("%d\n",dp[n][m]);
    }
    return 0;
}
时间: 2024-12-19 07:37:47

HDU 1159 Common Subsequence --- DP入门之最长公共子序列的相关文章

HDU 1159 Common Subsequence (动态规划、最长公共子序列)

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

hdu 1159 Common Subsequence(dp)

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

HDU 1159——Common Subsequence(DP)

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

POJ 1159 Palindrome &amp;&amp; HDU 1159 Common Subsequence

1.先说说杭电的1159吧! 这道题是基础动规,比较简单! 就是要你求最长的公共子序列(不要优化) 动态转移方程: dp[i+1][j+1]=(a[i]=b[i])?dp[i][j]+1:max(dp[i+1][j],dp[i][j+1]) AC代码: #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define N 520 char a[N],b[N]; in

hdu 1159 Common Subsequence(最长公共子序列 DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25416    Accepted Submission(s): 11276 Problem Description A subsequence of

hdu 1159 Common Subsequence(dp 最长公共子序列问题LCS)

最长公共子序列问题(LCS,Longerst Common Subsequence). s1s2……si+1和t1t2……tj+1的公共子序列可能是: ①当si+1=tj+1时,在s1s2……si+1和t1t2……tj+1的公共子序列末尾追加一个. ②s1s2……si+1和t1t2……tj的公共子序列 ③s1s2……si和t1t2……tj+1的公共子序列 所以易得到递推关系dp[i+1][j+1]=  max{ dp[i][j]+1 , dp[i][j+1] , dp[i+1][j]) }  

POJ 1458 &amp;&amp; HDU 1159 Common Subsequence (最長公共子序列)dp

鏈接:http://poj.org/problem?id=1458 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 subseque

hdu 1159 Common Subsequence(最长公共子序列,DP)

题意: 两个字符串,判断最长公共子序列的长度. 思路: 直接看代码,,注意边界处理 代码: char s1[505], s2[505]; int dp[505][505]; int main(){ while(scanf("%s%s",s1,s2)!=EOF){ int l1=strlen(s1); int l2=strlen(s2); mem(dp,0); dp[0][0]=((s1[0]==s2[0])?1:0); rep(i,1,l1-1) if(s1[i]==s2[0]) dp

HDU 1159 Common Subsequence(裸LCS)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 47676    Accepted Submission(s): 21890 Problem Description A subsequence of