zoukankan      html  css  js  c++  java
  • HDU 1159 Common Subsequence

    Common Subsequence

    Problem 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. 
    Sample Input
    abcfbc abfcab
    programming contest
    abcd mnp
     
    Sample Output
    4
    2
    0
     
    题目大意:求最长公共子序列
     
    代码如下:
     1 #include <iostream>
     2 # include<cstdio>
     3 # include<cstring>
     4 using namespace std;
     5 int max(int a,int b)
     6 {
     7     return a>b ?a : b;
     8 }
     9 char s1[1005],s2[1005];
    10 int dp[1005][1005];
    11 int main()
    12 {
    13     int i,j,len1,len2;
    14     while(scanf("%s%s",s1,s2)!=EOF)
    15     {
    16         len1 = strlen(s1);
    17         len2 = strlen(s2);
    18         int ans=0;
    19         dp[0][0] = 0;
    20         for(i=1; i<=len1; i++)
    21             dp[i][0] = 0;
    22         for(j=1; j<=len2; j++)
    23             dp[0][j] = 0;
    24         for(i=1; i<=len1; i++)
    25         {
    26             for(j=1; j<=len2; j++)
    27             {
    28                 if(s1[i-1]==s2[j-1])
    29                     dp[i][j] = max(max(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1]+1);
    30                 else
    31                     dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
    32                 if(dp[i][j]>ans)
    33                     ans= dp[i][j];
    34             }
    35         }
    36         printf("%d
    ",ans);
    37     }
    38     return 0;
    39 }
  • 相关阅读:
    eclipse快捷键
    go 中 var声明对比
    Post 中 Body 的 ContentType 用 Postman 举例
    MongoDB随笔(二) mongorestore恢复数据库
    MongoDB随笔(零) mongod配置 ...不断完善...会变得很长很长很长……
    MongoDB随笔(一)mac OSX下brew安装MongoDB
    mac OSX的 brew软件包管理器 相当于 centos下的yum
    2021-01-27 解决mac使用brew update更新无反应的问题(切换git地址)
    Ruby中实现module继承
    redmine问题集锦
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/3621486.html
Copyright © 2011-2022 走看看