• 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 }

  • 相关阅读:
    nginx配置二级目录,反向代理不同ip+端口
    一次实验环境中的数据库空间整理经历
    NFine中权限判断出错的问题
    Centos7 硬盘分区
    12个必备的JavaScript装逼技巧
    判断五大浏览器类型
    npm 常用命令及版本号浅析
    js 判断IE浏览器
    娱乐冷门小知识
    vue baidu Map 百度路书去掉动画
  • 原文地址:https://www.cnblogs.com/zsj576637357/p/2422426.html
Copyright © 2020-2023  润新知