zoukankan      html  css  js  c++  java
  • 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
    SampleInput
     1 #include<stdio.h>
     2 #include<string.h>
     3 #define max 505
     4 int main()
     5 {
     6     char s1[max],s2[max];
     7     int matrix[max][max],len1,len2;
     8     int i,j;
     9     while(scanf("%s%s",s1,s2)!=EOF)
    10     {
    11         len1=strlen(s1);
    12         len2=strlen(s2);
    13         for(i=0; i<=len1; i++)matrix[i][0]=0;
    14         for(j=0; j<=len2; j++)matrix[0][j]=0;
    15         for(i=1; i<=len1; i++)
    16         {
    17             for(j=1; j<=len2; j++)
    18             {
    19                 if(s1[i-1]==s2[j-1])
    20                 {
    21                     matrix[i][j]=matrix[i-1][j-1]+1;
    22                 }
    23                 else
    24                 {
    25                     if(matrix[i-1][j]>=matrix[i][j-1])
    26                     {
    27                         matrix[i][j]=matrix[i-1][j];
    28                     }
    29                     else
    30                     {
    31                         matrix[i][j]=matrix[i][j-1];
    32                     }
    33                 }
    34             }
    35         }
    36         printf("%d
    ",matrix[len1][len2]);
    37     }
    38     return 0;
    39 }
    View Code
    abcfbc abfcab
    programming contest 
    abcd mnp
    SampleOutput
    4
    2
    0
    题目大意:给出两个字符串,求两个字符串的最长公共字串。
  • 相关阅读:
    [SDOI 2009] 晨跑
    [ZJOI 2009] 假期的宿舍
    [BZOJ 2006] 狼抓兔子
    [BZOJ 1691] 挑剔的美食家
    [HNOI 2015] 菜肴制作
    [Codeforces 489E] Nastya and King-Shamans
    sum(sum(abs(y))) 中 sum(sum())什么意思?
    matlab 函数 bwarea
    系统启动挂载根文件系统时Kernel panic
    rmmod: chdir(/lib/modules): No such file or directory
  • 原文地址:https://www.cnblogs.com/to-creat/p/4891203.html
Copyright © 2011-2022 走看看