zoukankan      html  css  js  c++  java
  • HDU

    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. 

    Input

    abcfbc abfcab
    programming contest 
    abcd mnp

    Output

    4
    2
    0

    Sample Input

    abcfbc abfcab
    programming contest 
    abcd mnp

    Sample Output

    4
    2
    0

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <string>
     6 using namespace std;
     7 #define N 1005
     8 int dp[N+1][N+1];
     9 char str1[N],str2[N];
    10 int lcs(int len1,int len2)
    11 {
    12     int i,j;
    13     int len=max(len1,len2);
    14     for(i=0;i<=len;i++){
    15         dp[i][0]=0;
    16         dp[0][i]=0;
    17     }
    18     for(i=1;i<=len1;i++){
    19         for(j=1;j<=len2;j++){
    20             if(str1[i-1]==str2[j-1]){
    21                 dp[i][j]=dp[i-1][j-1]+1;
    22             }
    23             else {
    24                 dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
    25             }
    26         }
    27     }
    28     return dp[len1][len2];
    29 }
    30 int main()
    31 {
    32     while(cin>>str1>>str2){
    33         int len1=strlen(str1);
    34         int len2=strlen(str2);
    35         cout<<lcs(len1,len2)<<endl;
    36     }
    37     return 0;
    38 }
  • 相关阅读:
    [MySQL] 日志文件概述
    ASP.NET session expired simple solution
    Sailfish应用开发入门(一)ApplicationWindow与Cover
    linux下QT4的使用
    js控制图片定时切换不限制数量
    C++ 从零单排(3)
    wordcraft(陈高远)
    苦逼翻滚之实习找工记事产品岗(3.184.23长文慎入)
    【Oracle导入导出】expdp
    【leetcode】Minimum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/wydxry/p/7237863.html
Copyright © 2011-2022 走看看