zoukankan      html  css  js  c++  java
  • hdu 1159:Common Subsequence(动态规划)

    Common Subsequence

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


    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
     
    Source
     
    Recommend
    Ignatius   |   We have carefully selected several similar problems for you:  1087 1176 1058 1069 1421
     
    之前做过好多次,一直不解其意,最近重温一遍。现在写下解题心得。
    这道题的目的是求出a字符串和b字符串的最长公共子序列,用到动态规划。
    动态规划的解法:
      先定义两个字符数组存储两个字符串
    —— char a[1000]、b[1000];
      然后再定义一个二维数组,存储求解最终问题过程中产生的所有子问题的解
    —— int dp[1001][1001];
    最长公共子序列的状态转移方程为:
    if(a[i]==b[j])  
        dp[i][j]=dp[i-1][j-1]+1;
    else 
        dp[i][j]=dp[i-1][j]>dp[i][j-1]?dp[i-1][j]:dp[i][j-1];
    根据以上写出程序即可。
    另外摘取别人的一段对动态规划的解释:
    【动态规划法】
      经常会遇到复杂的问题不能简单的分解成几个子问题,而会分解出一系列的子问题。简单的采用把大问题分解成子问题,并综合所有子问题的解求出大问题的解的方法,问题求解耗时会按问题规模呈幂级数增加。
      为了节约重复求相同子问题的时间,引入一个数组,不管他们是否对最终解有用,把所有子问题的解存于数组中,这就是动态规划法所采用的基本做法。
     
    网易公开课的《算法导论》也有详细的讲解:
     
    下面给出代码:
    【C++】
     1 #include <iostream>
     2 
     3 using namespace std;
     4 int dp[2001][2001];
     5 int main()
     6 {
     7     //dp[i][j]代表着a取前i个字符和b取前j个字符时的最长公共子序列的大小
     8     char a[2001],b[2001];
     9     while(cin>>a>>b){
    10         int i,j;
    11         int al,bl;
    12         for(i=0;a[i]!='';i++);    //计算a、b字符串长度
    13         for(j=0;b[j]!='';j++);
    14         al=i;bl=j;
    15 
    16         for(i=0;i<=al;i++)   //dp[][]初始化
    17             dp[i][0]=0;
    18         for(i=0;i<=bl;i++)
    19             dp[0][i]=0;
    20 
    21         for(i=1;i<=al;i++)   //计算dp[][]
    22             for(j=1;j<=bl;j++){
    23                 if(a[i-1]==b[j-1])
    24                     dp[i][j]=dp[i-1][j-1]+1;
    25                 else
    26                     dp[i][j] = dp[i-1][j] > dp[i][j-1] ? dp[i-1][j] : dp[i][j-1];
    27             }
    28 
    29         cout<<dp[al][bl]<<endl;
    30     }
    31     return 0;
    32 }
    【C】
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 int dp[1001][1001];
     4 int main()
     5 {
     6     char a[1000],b[1000];
     7     while(scanf("%s%s",a,b)!=EOF){
     8         int i,j;
     9         int al,bl;
    10         for(i=0;a[i]!='';i++);
    11         for(j=0;b[j]!='';j++);
    12         al=i;bl=j;
    13         for(i=0;i<=al;i++)
    14             dp[i][0]=0;
    15         for(j=0;j<=bl;j++)
    16             dp[0][j]=0;
    17         for(i=1;i<=al;i++)
    18             for(j=1;j<=bl;j++){
    19                 if(a[i-1]==b[j-1])
    20                     dp[i][j] = dp[i-1][j-1]+1;
    21                 else
    22                     dp[i][j] = dp[i-1][j] > dp[i][j-1] ? dp[i-1][j] : dp[i][j-1];
    23             }
    24         printf("%d
    ",dp[al][bl]);
    25     }
    26     return 0;
    27 }

     

    Freecode : www.cnblogs.com/yym2013

  • 相关阅读:
    MP3文件格式解析
    各种流媒体服务器的架设(一)
    fread函数和fwrite函数
    [转]C#算法 一对小兔子一年后长成大兔子;一对大兔子每半年生一对小兔子。大兔子的繁殖期为4年,兔子的寿命是6年。假定第一年年初投放了一对小兔子,试编程计算,第n年末总共会有多少对兔子
    C#算法 质因数 最大公约数与最小公倍数
    数据库删除语句 Drop/Delete/Truncate比较
    [转]C# 截取指定长度的中英文混合字符串的算法
    C#算法 母牛从第4年起每年生一头小母牛,并且母牛不会死
    C#算法 有一个母羊,第2年和第4年可以生一头小母羊,在第5年死去,小母羊在它出生的第2年和第4年生小母羊,第5年死去
    C#算法 最值/平均
  • 原文地址:https://www.cnblogs.com/yym2013/p/3424314.html
Copyright © 2011-2022 走看看