uva--10405Longest Common Subsequence+dp

经典的最长公共子序列问题。

要注意的是题目中的输入会包含空格的情况,所以要用gets实现输入。

代码如下:

<span style="font-size:18px;">#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int dp[1100][1100];

int main()
{
    char str1[1100],str2[1100];
    int i,j;
    while(gets(str1+1))
    {
        gets(str2+1);
        int len1=strlen(str1+1);
        int len2=strlen(str2+1);
        memset(dp,0,sizeof(dp));
        for(i=1;i<=len1;i++)
            for(j=1;j<=len2;j++)
            {
                if(str1[i]==str2[j])
                    dp[i][j]=dp[i-1][j-1]+1;
                else
                    dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
            }
        int ans=0;
        for(i=1;i<=len1;i++)
            for(j=1;j<=len2;j++)
                ans=max(ans,dp[i][j]);
        printf("%d\n",ans);
    }
  return 0;
}</span>
时间: 2024-08-28 09:48:29

uva--10405Longest Common Subsequence+dp的相关文章

Uva 10405-Longest Common Subsequence(DP)

题目链接:点击打开链接 裸LCS 注意输入可能有空格 #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <string> #include <cctype> #include <vector> #include <cstdio> #include <cmath> #inclu

UVA 11404 Palindromic Subsequence[DP LCS 打印]

UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求LCS,转移同时维护f[i][j].s为当前状态字典序最小最优解 f[n][n].s的前半部分一定是回文串的前半部分(想想就行了) 当s的长度为奇时要多输出一个(因为这样长度+1,并且字典序保证最小(如axyzb  bzyxa,就是axb)) // // main.cpp // uva11404 //

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

Longest Common Subsequence (DP)

Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Example For "ABCD" and "EDCA", the LCS is "A" (or "D", "C"), return 1. For "ABCD" and &quo

POJ 1458 Common Subsequence DP

http://poj.org/problem?id=1458 用dp[i][j]表示处理到第1个字符的第i个,第二个字符的第j个时的最长LCS. 1.如果str[i] == sub[j],那么LCS长度就可以+1,是从dp[i - 1][j - 1] + 1,因为是同时捂住这两个相同的字符,看看前面的有多少匹配,+1后就是最大长度. 2.如果不同,那怎么办? 长度是肯定不能增加的了. 可以考虑下删除str[i] 就是dp[i - 1][j]是多少,因为可能i - 1匹配了第j个.也可能删除sub

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

uva 10405 Longest Common Subsequence (最长公共子序列)

uva 10405 Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, print the length of the longest common subsequence of both sequences. For example, the longest common subsequence of the following two sequences: abcdgh a

UVA 10405 Longest Common Subsequence

最长公共子系列,简单的dp,不过注意有空格,所以,在读字符串的时候,尽量用gets读,这样基本没问题 #include<iostream> #include<cstdio> #include<string> #include<cstring> using namespace std; int dp[1001][1001]; int MAX(int x,int y) { if (x>y) return x; else return y; } int ma

hdu-1159 Common Subsequence (dp中的lcs问题)

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