hduoj1159 dp,lcs

hduoj1159  dp,lcs

Common Subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25570    Accepted Submission(s):
11363

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 strictly increasing sequence <i1, i2, ..., ik> of
indices of X such that for all j = 1,2,...,k, xij = zj. For example, Z = <a,
b, f, c> is a subsequence of X = <a, b, c, f, b, c> with index sequence
<1, 2, 4, 6>. Given two sequences X and Y the problem is to find the
length of the maximum-length common subsequence of X and Y.
The program
input is from a text file. Each data set in the file contains two strings
representing the given sequences. The sequences are separated by any number of
white spaces. The input data are correct. For each set of data the program
prints on the standard output the length of the maximum-length common
subsequence from the beginning of a separate line.

Sample Input

abcfbc abfcab
programming contest
abcd mnp

Sample Output

4
2
0

求lcs,水题,用了滚动数组才过

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>

using namespace std;

const int maxn=1000100;
char s[maxn],t[maxn];
int dp[2][maxn];

int main()
{
    while(scanf("%s%s",s,t)!=EOF){
        int ls=strlen(s),lt=strlen(t);
        memset(dp,0,sizeof(dp));
        char *ss=s-1,*tt=t-1;
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=ls;i++){
            for(int j=1;j<=lt;j++){
                if(ss[i]==tt[j]) dp[i%2][j]=dp[(i+1)%2][j-1]+1;
                else dp[i%2][j]=max(dp[(i+1)%2][j],dp[i%2][j-1]);
            }
        }
        printf("%d\n",dp[ls%2][lt]);
    }
    return 0;
}

时间: 2024-10-10 17:43:04

hduoj1159 dp,lcs的相关文章

poj1458——dp,lcs

poj1458——dp,lcs Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 40529   Accepted: 16351 Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence

poj 2250 Compromise dp lcs 路径输出

点击打开链接题目链接 Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6520   Accepted: 2922   Special Judge Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria

UVA-1625-Color Length(DP LCS变形)

Color Length(UVA-1625)(DP LCS变形) 题目大意 输入两个长度分别为n,m(<5000)的颜色序列.要求按顺序合成同一个序列,即每次可以把一个序列开头的颜色放到新序列的尾部. https://odzkskevi.qnssl.com/a68cbd3e27f46b4f02ea12b7b1a1abca 然后产生的新序列中,对于每一个颜色c,都有出现的位置,L(c)表示最小位置和最大位置之差,求L(c)总和最小的新序列. 分析 LCS 是公共上升子序列,在动态转移的过程中,考虑

POJ 1159 Palindrome(DP LCS)

Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into t

poj1080--Human Gene Functions(dp:LCS变形)

Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17206   Accepted: 9568 Description It is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simply denoted by four

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 //

LightOJ1033 Generating Palindromes(区间DP/LCS)

题目要计算一个字符串最少添加几个字符使其成为回文串. 一年多前,我LCS这道经典DP例题看得还一知半解时遇到一样的问题,http://acm.fafu.edu.cn/problem.php?id=1007. 当时完全靠自己瞎YY出了LCS的解法: 我当时这么想的: 把字符串分成两个部分,假设这两个部分分别就属于新的回文串的对称的两部分,那么要添加最少的字符串形成回文串就需要前一部分和后一部分公共部分最多,也就是它们的(一正一反)LCS. 这样就是枚举中间部分的位置(另外还要分情况讨论奇数和偶数回

POJ 1458 Common Subsequence (DP+LCS,最长公共子序列)

题意:给定两个字符串,让你找出它们之间最长公共子序列(LCS)的长度. 析:很明显是个DP,就是LCS,一点都没变.设两个序列分别为,A1,A2,...和B1,B2..,d(i, j)表示两个字符串LCS长度. 当A[i] = B[j] 时,这个最长度就是上一个长度加1,即:d(i, j) = d(i-1, j-1) + 1; 当A[i] != B[j] 时,那就是前面的最长长度(因为即使后面的不成立,也不会影响前面的),即:d(i, j) = max{d(i-1, j), d(i, j-1)}

POJ 2250 Compromise (线性dp LCS +递归路径)

Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6735   Accepted: 3009   Special Judge Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fu