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