题目链接:https://vjudge.net/contest/68966#problem/L
题目:
给定序列的子序列是给定序列,其中省略了一些元素(可能没有)。给定序列X = <x1,x2,...,xm>另一个序列Z = <z1,z2,...,zk>是X的子序列,如果存在严格增加的序列<i1,i2,..对于所有j = 1,2,...,k,x ij = zj,X的索引,...,ik>。例如,Z = <a,b,f,c>是X = <a,b,c,f,b,c>的子序列,其索引序列<1,2,4,6>。给定两个序列X和Y,问题是找到X和Y的最大长度公共子序列的长度。
输入
程序输入来自std输入。输入中的每个数据集包含两个表示给定序列的字符串。序列由任意数量的空格分隔。输入数据是正确的。
产量
abcfbc abfcab programming contest abcd mnpSample Output
4 2 0
思路:通过这个去看了下LCS:
//
// Created by hanyu on 2019/8/8.
//
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include<math.h>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=1000+7;
#define MAX 0x3f3f3f3f
int main()
{
char s[maxn],s1[maxn];
int dp[maxn][maxn];
while(~scanf("%s%s",s,s1))
{
int n=strlen(s);
int n1=strlen(s1);
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n1;j++)
{
if(s[i-1]==s1[j-1])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
printf("%d\n",dp[n][n1]);
}
return 0;
}
[kuangbin带你飞]专题十二 基础DP1L - Common Subsequence POJ - 1458(LCS最长公共子序列)
原文:https://www.cnblogs.com/Vampire6/p/11321237.html