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 }
  • 相关阅读:
    CSS复合选择器
    CSS样式规则及字体样式
    jQuery 样式操作
    jQuery 选择器
    jQuery 的基本使用
    jQuery 介绍
    本地存储
    移动端常用开发框架
    移动端常用开发插件
    移动端click 延时解决方案
  • 原文地址:https://www.cnblogs.com/wydxry/p/7237863.html
Copyright © 2011-2022 走看看