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 }
  • 相关阅读:
    VTK 空间几何变换(Transform),平移、旋转和缩放
    VTK 对象绘制 3D坐标轴(vtkCubeAxesActor)
    VTK 颜色映射之颜色插值
    VTK 模型的颜色映射
    VTK 如何不使用qvtkwidget,而直接把vtk出口嵌入qt窗口中
    VTK vtkRenderWindow 与vtkRenderWindowInteractor详解
    Qt 将界面程序封装成动态库DLL
    Qt 界面做成dll以便调用
    Qt 将带界面的程序封装成dll
    Qt 开发的界面程序封装成DLL,在VC中成功调用
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/3621486.html
Copyright © 2011-2022 走看看