zoukankan      html  css  js  c++  java
  • 公共子序列(动规)

    公共子序列

    总时间限制: 
    1000ms
     
    内存限制: 
    65536kB
    描述
    我们称序列Z = < z1, z2, ..., zk >是序列X = < x1, x2, ..., xm >的子序列当且仅当存在 严格上升 的序列< i1, i2, ..., ik >,使得对j = 1, 2, ... ,k, 有xij = zj。比如Z = < a, b, f, c > 是X = < a, b, c, f, b, c >的子序列。

    现在给出两个序列X和Y,你的任务是找到X和Y的最大公共子序列,也就是说要找到一个最长的序列Z,使得Z既是X的子序列也是Y的子序列。
    输入
    输入包括多组测试数据。每组数据包括一行,给出两个长度不超过200的字符串,表示两个序列。两个字符串之间由若干个空格隔开。
    输出
    对每组输入数据,输出一行,给出两个序列的最大公共子序列的长度。
    样例输入
    abcfbc         abfcab
    programming    contest 
    abcd           mnp
    
    样例输出
    4
    2
    0
    
    来源
    翻译自Southeastern Europe 2003的试题
    【代码】
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    using namespace std;
    int f[201][201];
    int max(int a,int b)
    {
        return a>b?a:b;
    }
    int main()
    {
        string s1,s2;
         while(cin>>s1>>s2)
         {
        int m=s1.length();
        int n=s2.length();
        for(int i=1;i<=m;i++)
        for(int j=1;j<=n;j++)
        {
            if(s1[i-1]==s2[j-1])
            f[i][j]=f[i-1][j-1]+1;
            else
            f[i][j]=max(f[i-1][j],f[i][j-1]);
        }
        printf("%d
    ",f[m][n]);
         }
        return 0;
    }
  • 相关阅读:
    606. Construct String from Binary Tree
    557. Reverse Words in a String III
    551. Student Attendance Record I
    541. Reverse String II
    521. Longest Uncommon Subsequence I
    520. Detect Capital
    459. Repeated Substring Pattern
    人脸检测源码facedetection
    人脸检测的model类facemodel
    人脸检测解析json的工具类face_test
  • 原文地址:https://www.cnblogs.com/zzyh/p/6683706.html
Copyright © 2011-2022 走看看