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): 38176    Accepted Submission(s): 17504


    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
     
    动态规划中的最长公共子序列。用dp[i][j]保存第一个字符串前i个字符和第二个字符串前j个字符的公共子序列长度。
    如果str1[i]==str2[j],dp[i][j]=dp[i-1][j-1]。
    如果str1[i]!=str2[j],dp[i][j]=max(dp[i-1][j],dp[i][j-1])
    上述即是动态规划的状态转移公式。
     
     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 using namespace std;
     5 
     6 int dp[1001][1001];
     7 
     8 char str1[1001], str2[1001];
     9 
    10 int max(int a, int b)
    11 {
    12     if(a>b)
    13         return a;
    14     else
    15         return b;
    16 }
    17 
    18 int main()
    19 {
    20     while(scanf("%s %s", str1, str2)!=EOF)
    21     {
    22 
    23         int len1=strlen(str1);
    24         int len2=strlen(str2);
    25 
    26 
    27         for(int i=0; i<=len1; i++) dp[i][0]=0;
    28         for(int j=0; j<=len2; j++) dp[0][j]=0;
    29 
    30         for(int i=1; i<=len1; i++)
    31             for(int j=1; j<=len2; j++)
    32         {
    33             if(str1[i-1]!=str2[j-1])
    34             {
    35                 dp[i][j]=max(dp[i-1][j], dp[i][j-1]);
    36 
    37             }
    38 
    39             else
    40             {
    41                 dp[i][j]=dp[i-1][j-1]+1;
    42             }
    43         }
    44 
    45     printf("%d
    ",dp[len1][len2]);
    46     }
    47 
    48     return 0;
    49 }
  • 相关阅读:
    代码生成器
    怎样成为优秀的软件模型设计者[转]
    2010年部分节假日安排的通知
    Web开发工具整理
    JS模版
    Timer_MinBytesPerSecond,Timer_ConnectionIdle解决办法
    [原]Asp.Net提交后,刷新时事件触发问题解决
    [转]JS日期选择控件
    知道了sa密码,控制服务器
    调试
  • 原文地址:https://www.cnblogs.com/shenckicc/p/6781235.html
Copyright © 2011-2022 走看看