• Common Subsequence


    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.
     

    Input

    abcfbc abfcab programming contest abcd mnp
     

    Output

    4 2 0
     

    Sample Input

    abcfbc abfcab programming contest abcd mnp
     

    Sample Output

    4 2 0
     
    最裸的最长公共子序列不解释
    View Code
     1 #include<stdio.h>
    2 #include<stdlib.h>
    3 #include<string.h>
    4 char str1[10005], str2[10005];
    5 int dp[1005][1005];
    6
    7 int maxx( int x, int y )
    8 {
    9 return x > y ? x : y;
    10 }
    11
    12 int deal( int len1, int len2 )
    13 {
    14 int len = maxx( len1, len2 );
    15 for( int i = 0; i < len; i++ )
    16 {
    17 dp[i][0] = 0;
    18 dp[0][i] = 0;
    19 }
    20 for( int i = 1; i <= len1; i++ )
    21 for( int j = 1; j <= len2; j++ )
    22 {
    23 if( str1[i-1] == str2[j-1] )
    24 {
    25 dp[i][j] = dp[i-1][j-1]+1;
    26 }
    27 else
    28 {
    29 dp[i][j] = maxx( dp[i][j-1], dp[i-1][j] );
    30 }
    31 }
    32 return dp[len1][len2];
    33 }
    34
    35 int main()
    36 {
    37 while( scanf( "%s%s", str1, str2 ) == 2 )
    38 {
    39 int len1 = strlen( str1 );
    40 int len2 = strlen( str2 );
    41 printf( "%d\n", deal( len1, len2 ) );
    42 }
    43 }

  • 相关阅读:
    电脑U盘启动制作
    windows系统使用
    CentOS升级Openssl至openssl-1.1.0
    PHP编译安装时常见错误解决办法
    阿里 Linux服务器外网无法连接MySQL解决方法
    centos 下 sphinx安装和配置
    集成百度编辑器 ueditor 后端配置项没有正常加载,上传插件不能正常使用!
    nginx 环境 thinkphp 隐藏index.php
    在 Linux 下搭建 Git 服务器
    MySQL远程连接不上的解决方法
  • 原文地址:https://www.cnblogs.com/zsj576637357/p/2422426.html
Copyright © 2020-2023  润新知