zoukankan      html  css  js  c++  java
  • HDU 1159 Common Subsequence

    Common Subsequence

    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
     
    题目大意:求最长公共子序列
     
    代码如下:
     1 #include <iostream>
     2 # include<cstdio>
     3 # include<cstring>
     4 using namespace std;
     5 int max(int a,int b)
     6 {
     7     return a>b ?a : b;
     8 }
     9 char s1[1005],s2[1005];
    10 int dp[1005][1005];
    11 int main()
    12 {
    13     int i,j,len1,len2;
    14     while(scanf("%s%s",s1,s2)!=EOF)
    15     {
    16         len1 = strlen(s1);
    17         len2 = strlen(s2);
    18         int ans=0;
    19         dp[0][0] = 0;
    20         for(i=1; i<=len1; i++)
    21             dp[i][0] = 0;
    22         for(j=1; j<=len2; j++)
    23             dp[0][j] = 0;
    24         for(i=1; i<=len1; i++)
    25         {
    26             for(j=1; j<=len2; j++)
    27             {
    28                 if(s1[i-1]==s2[j-1])
    29                     dp[i][j] = max(max(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1]+1);
    30                 else
    31                     dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
    32                 if(dp[i][j]>ans)
    33                     ans= dp[i][j];
    34             }
    35         }
    36         printf("%d
    ",ans);
    37     }
    38     return 0;
    39 }
  • 相关阅读:
    dmo4解析xml
    myeclise生成webservice客户端代码
    华为QOS原理及配置
    【转载】 Jointwave零延时视频传输for FPGA/ASIC进入军工领域
    【转载】 网络性能测试工具
    【转载】 结构体大小计算
    【转载】 H264的I/P/B帧类型判断
    【转载】 IP实时传输协议RTP/RTCP详解
    【转载】 了解实时媒体的播放(RTP/RTCP 和 RTSP)
    【转载】 CSDN博客与博客园使用对比
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/3621486.html
Copyright © 2011-2022 走看看