zoukankan      html  css  js  c++  java
  • C

    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

    题意:给你两组或多组子序列,现在让你求出他们的最大公共子序列的长度

    分析:做这个提前我事先看了书知道这是个最大公共子序列问题,然后就根据书上给出的转移方程很顺利的就将此题AC了,解决本题的关键就是你要得到那个转移方程。
    知识点:最大公共子序列问题。

    心得:开数组的时候不能大意啊,一定要按题目给定的范围来定义

    AC代码:
    解法一:
    #include <iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int d[1005][1005];
    char a[1005],b[1005];
    int max(int x,int y)
    {
        return x>y?x:y;
    }
    int main()
    {
    
        while(scanf("%s %s",a,b)!=EOF)
        {
            memset(d,0,sizeof(d));
           int m=strlen(a);//数组长度
           int n=strlen(b);
           for(int i=1;i<=m;i++)
            for(int j=1;j<=n;j++)
           {
               if(a[i-1]==b[j-1])
                d[i][j]=d[i-1][j-1]+1;
               else d[i][j]=max(d[i-1][j],d[i][j-1]);//最长公共子序列转移方程
           }
           printf("%d
    ",d[m][n]);
        }
        return 0;
    }
    解法二:

    #include<iostream>/*编译有问题,运行错误,提交用C++一直时格式错误,用G++一下就过了,好坑啊*/
    #include<cstdio>
    //#include<cstring>
    using namespace std;
    const int MAX=500;//数组开大会导致运行错误,没弄懂。就是主函数内的数组不能开太大,所以一般定义数组最好定义在主函数外面
    int main()
    {
        string a,b;
        int d[MAX][MAX];
        while(cin>>a>>b)
        {
           int m=a.length();//数组长度
            int n=b.length();
             //memset(d,0,sizeof(d));
            for(int i=1; i<=m; i++)
                for(int j=1; j<=n; j++)
                {
                    if(a[i-1]==b[j-1])
                        d[i][j]=d[i-1][j-1]+1;
                    else if(d[i-1][j]>d[i][j-1])
                        d[i][j]=d[i-1][j];
                        else d[i][j]=d[i][j-1];
                }
            cout<<d[m][n]<<endl;
        }
        return 0;
    }


  • 相关阅读:
    Web2.0技能评测
    [收藏]流程设计和优化原则
    [读书笔记1] 卓有成效的管理者(彼得.德鲁克)
    [读书笔记3] 卓有成效的管理者聚焦贡献
    [读书笔记2] 卓有成效的管理者管理时间
    动态生成的Web软件 应该如何设计???
    Logs
    JQuery推荐插件(200+)
    Spring AOP 实例
    《JavaScript凌厉开发Ext详解与实践》一书说了些什么
  • 原文地址:https://www.cnblogs.com/lbyj/p/5758066.html
Copyright © 2011-2022 走看看