Problem B(子序列问题)

Problem Description

A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = &lt;x1, x2, ..., xm&gt; another sequence Z = &lt;z1, z2, ..., zk&gt; is a subsequence of X if there exists a strictly increasing sequence &lt;i1, i2, ..., ik&gt; of indices of X such that for all j = 1,2,...,k, xij = zj. For example, Z = &lt;a, b, f, c&gt; is a subsequence of X = &lt;a, b, c, f, b, c&gt; with index sequence &lt;1, 2, 4, 6&gt;. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y. <br>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. <br>

Sample Input

abcfbc abfcab

programming contest

abcd mnp

Sample Output

4

2

0

题意:给出两组字符串,找出两个字符串中最大个数相同的字符。

思路:设有字符串a[0...n],b[0...m],字符串a对应的是二维数组num的行,字符串b对应的是二维数组num的列。然后每次记录比较后的状态

代码:

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

char str1[1005];
char str2[1005];
int dp[1005][1005];

int main()
{
  int i,j;
  int len1,len2;
  while(cin>>str1>>str2)
  {
    len1=strlen(str1);
    len2=strlen(str2);
    for(i=0;i<len1;i++)
    {
      dp[i][0]=0;
    }
    for(i=0;i<len2;i++)
    {
      dp[0][i]=0;
    }
    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;
        }
        else
        {
          dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
        }
      }
    }
    cout<<dp[len1][len2]<<endl;
  }
  return 0;
}

本题有多种方法可以做

时间: 2024-09-05 20:18:34

Problem B(子序列问题)的相关文章

fzuoj Problem 2129 子序列个数

http://acm.fzu.edu.cn/problem.php?pid=2129 Problem 2129 子序列个数 Accept: 162    Submit: 491Time Limit: 2000 mSec    Memory Limit : 32768 KB  Problem Description 子序列的定义:对于一个序列a=a[1],a[2],......a[n].则非空序列a'=a[p1],a[p2]......a[pm]为a的一个子序列,其中1<=p1<p2<..

FZUProblem 2129 子序列个数(dp)

 Problem 2129 子序列个数 Accept: 147    Submit: 432 Time Limit: 2000 mSec    Memory Limit : 32768 KB  Problem Description 子序列的定义:对于一个序列a=a[1],a[2],......a[n].则非空序列a'=a[p1],a[p2]......a[pm]为a的一个子序列,其中1<=p1<p2<.....<pm<=n. 例如4,14,2,3和14,1,2,3都为4,1

poj_1458 LCS problem F.最长上升公共子序列

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

DP 动态规划 Problem B 1002 求最长上升子序列的长度

Problem B  ID:1002 简单题意:给出X和Z两个字符串,求最长上升子序列的长度. 解题思路形成过程:利用矩阵.X字符串中的各字符依次作为行标,Z字符串中的各字符依次作为列标. 从第一行第一列开始逐行遍历:如果当前位置对应的两个字符相同,则在这个位置记录"前一行前一列"的对应的数+1: 如果当前位置对应的两个字符不同,则在这个位置记录"此行前一列"和"此列前一行"对应的两个数的最大值. 遍历结束后,最后一行最后一列获得的数便是最长上升

2017ICPC南宁赛区网络赛 The Heaviest Non-decreasing Subsequence Problem (最长不下降子序列)

Let SSS be a sequence of integers s1s_{1}s?1??, s2s_{2}s?2??, ........., sns_{n}s?n?? Each integer is is associated with a weight by the following rules: (1) If is is negative, then its weight is 000. (2) If is is greater than or equal to 10000100001

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

hdu1231 最大连续子序列

最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 22849    Accepted Submission(s): 10135 Problem Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j

HDU 3998 Sequence (最长递增子序列+最大流SAP,拆点法)经典

Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1666    Accepted Submission(s): 614 Problem Description There is a sequence X (i.e. x[1], x[2], ..., x[n]). We define increasing subsequ

HDU 1003 Max Sum【动态规划求最大子序列和详解 】

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 250714    Accepted Submission(s): 59365 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max su