zoukankan      html  css  js  c++  java
  • HDU 1159 Common Subsequence 动态规划

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159

    Common Subsequence

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


    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
     

    解题思路:动态规划方程dp[i][j] = max ( dp[i-1][k] ) (0 <= k < j) + 1;

    解题代码:G++

     1 // File Name: Common Subsequence 1159.cpp
     2 // Author: sheng
     3 // Created Time: 2013年05月14日 星期二 15时48分11秒
     4 
     5 #include <cstdio>
     6 #include <string.h>
     7 #include <iostream>
     8 using namespace std;
     9 
    10 const int max_n = 1010;
    11 char str1[max_n], str2[max_n];
    12 int dp[2][max_n]; //滚动数组dp
    13 
    14 int main()
    15 {
    16 
    17     int Max;
    18     while (~scanf ("%s %s", str1, str2))
    19     {
    20         int len1 = strlen (str1);
    21         int len2 = strlen (str2);
    22         memset (dp, 0, sizeof (dp));
    23         int pos = 1; //滚动数组初始使用
    24         for (int i = 0; i < len1; i ++)
    25         {
    26             for (int j = 0; j < len2; j ++)
    27             {
    28                 if (j == 0 || i == 0)
    29                     Max = 0;
    30                 else
    31                 {
    32                     Max = max (dp[pos^1][j-1], Max);
    33                 } 
    34                 if (str1[i] == str2[j])
    35                     dp[pos][j] = Max + 1;
    36                 else dp[pos][j] = dp[pos^1][j];
    37             }
    38             pos ^= 1;//交换数组
    39         }
    40         pos ^= 1;
    41         for (int i = 0; i < len2; i ++)
    42         {
    43             Max = max (dp[pos][i], Max);
    44         }
    45         printf ("%d\n", Max);
    46     }
    47     return 0;
    48 }
    View Code G++
  • 相关阅读:
    string 流
    文件输入和输出
    IO类
    算法
    MySQL常用处理方法
    linux curl工具
    设计模式
    C语言编程流程
    js escape 与php escape
    js undefined易错分析
  • 原文地址:https://www.cnblogs.com/shengshouzhaixing/p/3078152.html
Copyright © 2011-2022 走看看