zoukankan      html  css  js  c++  java
  • poj1458 dp入门

    Common Subsequence
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 37551   Accepted: 15023

    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.

    Input

    The program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.

    Output

    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

    Source

    入门dp,主要是理解动归的思考方式,把串变断,比如先确定两个串从头开始的第一个字符相同和不相同两种状态下对后面有什么影响,然后想想怎么描述两个串公共子序列这个状态,我们这里Maxsum[i][j]表示(0~i)和(0~j)两个串当前情况下最长公共子序列的长度,考虑最小子问题情况,第一个字符相同,则Maxsum[i+1][j+1] = Maxsum[i][j]+1;
    考虑初始状态,很容易想到,Maxsum[0][len1]和Maxsum[len2][0]是不可能有公共子序列的,为0,。

    Maxsum(i,j)不会比Maxsum(i,j-1)
    和Maxsum(i-1,j)两者之中任何一个小,也不会比两者都大。

     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     char str1[1001],str2[1001];
     8     while(scanf("%s%s",str1,str2)!=EOF)
     9     {
    10         int len1 = strlen(str1);
    11         int len2 = strlen(str2);
    12         int Maxsum[1001][1001];  //Maxsum[i][j] ,i表示长度为i的串一,j表示长度为j的串二,Maxsum[i][j]两串最大公共子序列
    13         for(int i=0;i<len1;i++)
    14         {
    15             Maxsum[i][0] = 0;
    16         }
    17         for(int j=0;j<len2;j++)
    18         {
    19             Maxsum[j][0] = 0;
    20         }
    21 
    22         for(int i=0;i<len1;i++)
    23             for(int j=0;j<len2;j++)
    24                 if(str1[i]==str2[j])
    25                     Maxsum[i+1][j+1] = Maxsum[i][j] +1;
    26                 else{
    27                     Maxsum[i+1][j+1] = max(Maxsum[i][j+1],Maxsum[i+1][j]);
    28                 }
    29         printf("%d
    ",Maxsum[len1][len2]);
    30     }
    31     return 0;
    32 }
  • 相关阅读:
    又爱又恨的eval
    http_build_query 这个方法会把值为NULL的给干掉
    allow_url_fopen设置
    纠结了下 B 和 STRONG标签区别
    Drupal 发邮件模块 drupal smtp 安装与设置
    php array_merge 和 两数组相加区别
    学历严格正相关于素质 Kai
    表语就是主语补语,靠 Kai
    一些真正有思想的up Kai
    光速不变且最大,换个思路想,非常合理,犹如天经地义【转载】 Kai
  • 原文地址:https://www.cnblogs.com/jhldreams/p/3852346.html
Copyright © 2011-2022 走看看