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;
    }


  • 相关阅读:
    进程和线程的一个简单形象的解释
    java流的性能优化1-文件复制
    JAVA修饰符类型(public,protected,private,friendly)
    Codeforces Round #252 (Div. 2) 441B. Valera and Fruits
    游戏开场镜头拉近(Unity3D开发之四)
    P1282 多米诺骨牌
    P1280 尼克的任务
    求最小生成树(暴力法,prim,prim的堆优化,kruskal)
    暴力求最短路
    用MyEclipse将java文件转换成UML类图
  • 原文地址:https://www.cnblogs.com/lbyj/p/5758066.html
Copyright © 2011-2022 走看看