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
*/


题目大意:输入两个序列,一个序列是X,一个序列是Y,查询X,Y中最长的相同的公共子序列


/*
思路分析:


设c[i][j]为字符串a的第i个字符与字符串b的第j个字符为止的最长公共子序列长度,那么有两种情况:


当a[i] == b[j]时,c[i][j]应该是前一个状态的最长公共子序列长度 + 1,而前一个状态是c[i - 1][j]呢,
还是c[i][j - 1]?两者都不是,因为a[i]与b[j]匹配,a[i]与b[j]必然不能已经匹配过,否则就是同一个字母匹配了多次,
这必然是非法的,因此上一个状态应是c[i - 1][j - 1],即c[i][j] = c[i - 1][j - 1] + 1;
当a[i] != b[j]时,上一个状态可能是c[i - 1][j]或c[i][j - 1],而既然要找最长公共子序列,自然是找最大的一个,
即c[i][j] = max(c[i - 1][j], c[i][j - 1])。






方法一:一直是worry answer,不知道为什么,还希望看到的大牛给予指出
#include<iostream>
#include<string>
using namespace std;
char X[1000];
char Y[1000];
int c[1000][1000];
int main()
{
int i=0,j=0;
while(cin>>X>>Y)
{
int n=strlen(X);
int n1=strlen(Y);
for(i=0;i<n;i++)
{
for(j=0;j<n1;j++)
{
if(X[i]==Y[j])

if(i-1<0||j-1<0)
c[i][j]=0+1;
else 
 c[i][j]=c[i-1][j-1]+1;
}
else c[i][j]=max(c[i-1][j],c[i][j-1]);
}
}
cout<<c[n-1][n1-1]<<endl;
}
return 0;
}


方法二:
#include <iostream> 
#include <cstring> 
using namespace std;


char X[1000]; 
char Y[1000]; 
int dp[1000][1000];


int main() 

    int i,j; 
    int len1,len2; 
    while(cin>>X>>Y) 
    { 
        len1=strlen(X); 
        len2=strlen(Y); 
        for(i=0;i<len1;i++) 
        { 
            dp[i][0]=0; 
        } 
        for(i=0;i<len2;i++) 
        { 
            dp[0][i]=0; 
        } 
        for(i=1;i<=len1;i++) //这里把第一行当成了第0行去算
        { 
            for(j=1;j<=len2;j++) 
            { 
                if(X[i-1]==Y[j-1]) 
                { 
                    dp[i][j]=dp[i-1][j-1]+1; 
                } 
                else 
                { 
                    dp[i][j]=max(dp[i-1][j],dp[i][j-1]); 
                } 
            } 
        } 
        cout<<dp[len1-1][len2-1]<<endl; //这里本身应该是len-1行,但是因为上面的第一行当成第0行,所以这里的就应该是最后一行当成是len-1

    return 0; 
}




//方法三:
#include<iostream>
#include<string>
using namespace std;
int c[1001][1001];
int getc(int i, int j){
        if(i >= 0 && j >= 0)
                return c[i][j];
        else
                return 0;
}


int main(){
        string a, b;
        while(cin >> a >> b){
//一下是算法最主要的部分:
                for(int i = 0; i<a.length(); ++i){
                        for(int j = 0; j < b.length(); ++j){                               
if(a[i] == b[j]){
                                        c[i][j] = getc(i-1,j-1)+1;
                                }else{
                                        c[i][j] = max(getc(i-1,j),getc(i,j-1));
                                }
                        }
                }
                cout << c[a.length() - 1][b.length() - 1] << endl;
        }
        return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
"Analyzing computer system performance is often regarded by most system administrators, IT professionals and software engineers as a black art that is too time consuming to learn and apply. Finally, this book by acclaimed performance analyst Dr. Neil Gunther makes this subject understandable and applicable through programmatic examples. The means to this end is the open-source performance analyzer Pretty Damn Quick (PDQ) written in Perl and available for download from the author’s Website: www.perfdynamics.com. As the epigraph in this book points out, Common sense is the pitfall of performance analysis. The performance analysis framework that replaces common sense is revealed in the first few chapters of Part I. The important queueing concepts embedded in PDQ are explained in a very simple style that does not require any knowledge of formal probability theory. Part II begins with a full specification of how to set up and use PDQ replete with examples written in Perl. Subsequent chapters present applications of PDQ to the performance analysis of multicomputer architectures, benchmark results, client/server scalability, and Web-based applications. The examples are not mere academic toys but are based on the author's experience analyzing the performance of large-scale systems over the past 20 years. By following his lead, you will quickly be able to set up your own Perl scripts for collecting data and exploring performance-by-design alternatives without inflating your manager’s schedule."

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值