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, x ij = 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.

Input

The program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.

Output

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
题解:DP,最大公共子序列;dp[i][j]表示第一个串的第i个字符,第二个串的第j个字符所能匹配的最长公共子串。if s1[i]==s2[j] dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(dp[i-1][j],dp[i][j-1])找最大值即可:
参考代码为:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std; int main()
{
string str1, str2;
while (cin >> str1 >> str2)
{
int l1 = str1.size();
int l2 = str2.size();
int dp[1010][1010]={0};
int Max = 0; for (int i = 0; i<l1; i++)
{
for (int j = 0; j<l2; j++)
{
if (str1[i] == str2[j])
{
dp[i+1][j+1] = dp[i][j] + 1;
if (dp[i+1][j+1]>Max)
Max = dp[i+1][j+1]; }
else
{
dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j]);
if (dp[i + 1][j + 1]>Max)
Max = dp[i + 1][j + 1];
}
}
}
cout << Max << endl; }
return 0;
}

  

POJ1458 Subsquence的更多相关文章

  1. 算法实践--最长递增子序列(Longest Increasing Subsquence)

    什么是最长递增子序列(Longest Increasing Subsquence) 对于一个序列{3, 2, 6, 4, 5, 1},它包含很多递增子序列{3, 6}, {2,6}, {2, 4, 5 ...

  2. 最长公共子序列LCS(POJ1458)

    转载自:https://www.cnblogs.com/huashanqingzhu/p/7423745.html 题目链接:http://poj.org/problem?id=1458 题目大意:给 ...

  3. 最长公共子序列(POJ1458)

    题目链接:http://poj.org/problem?id=1458 题目大意:给出两个字符串,求出这样的一个最长的公共子序列的长度:子序列中的每个字符都能在两个原串中找到,而且每个字符的先后顺序和 ...

  4. DP---(POJ1159 POJ1458 POJ1141)

    POJ1159,动态规划经典题目,很适合初学者入门练手. 求:为了使字符串左右对称,应该插入的最小字符数目. 设字符串为S1 S2 S3 - Sn. 这个字符串有n个字符,根据DP的基本思路,减少问题 ...

  5. POJ-1458(LCS:最长公共子序列模板题)

    Common Subsequence POJ-1458 //最长公共子序列问题 #include<iostream> #include<algorithm> #include& ...

  6. poj1458 Common Subsequence ——最长公共子序列

    link:http://poj.org/problem?id=1458 最基础的那种 #include <iostream> #include <cstdio> #includ ...

  7. poj1458

    //Accepted 4112 KB 16 ms //最长公共子串 #include <cstdio> #include <cstring> #include <iost ...

  8. HDU1159 && POJ1458:Common Subsequence(LCS)

    Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...

  9. poj1458 求最长公共子序列 经典DP

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 45763   Accepted: 18 ...

随机推荐

  1. C语言:大数取余

    大数取余数(数组) 今天做学校的oj时遇到一题,问题可见一下截图: 查遍各大论坛,都没有遇到合适的方法,普通方法不可用,要采用数组的形式. 被除数超过long long类型,不能采用常规思路,否则会出 ...

  2. celery 启用worker ValueError: not enough values to unpack

    [2018-01-12 19:08:15,545: INFO/MainProcess] Received task: tasks.add[5d387722-5389-441b-9b01-a619b93 ...

  3. 从壹开始 [ Design Pattern ] 之一 ║ 设计模式开篇讲

    缘起 不说其他的没用的开场白了,直接给大家分享三个小故事,都来自于我的读者粉丝(我厚着脸皮称为粉丝吧

  4. Pashmak and Graph(dp + 贪心)

    题目链接:http://codeforces.com/contest/459/problem/E 题意:给一个带权有向图, 找出其中最长上升路的长度. 题解:先按权值对所有边排序, 然后依次 u -& ...

  5. 开始逆向objc基础准备(二)我的平台是simulator-x86_64

    建项目运行中断调试,lldb中显示寄存器看到有rax-r15, stm0-stm7, xmm0-xmm15, ymm0-ymm15,即为x64体系支持sse4. 再在lldb中查看寄存器别名得到以下对 ...

  6. Openlayers Overlay加载gif图片

    说明: 项目中使用vector图层做图片撒点功能,发现加载gif没有效果.网上查找资料发现,openlayers不支持gif图片样式. 后面采用overlay的方式,gif图片赋值给DOM元素 解决方 ...

  7. 2019-10-30,Hack The Box 获取邀请码

    一.快速获取邀请码方法 1,使用curl请求下面的地址curl -X POST https://www.hackthebox.eu/api/invite/generate 2,在返回结果的code部分 ...

  8. scss--函数 (Functions)--unit

    (Sass::Script::Value::String) unit($number) Returns the unit(s) associated with a number. Complex un ...

  9. Selenium多层级的iframe中元素的定位

    很多时候我们遇到多层级的iframe就会想各种方法去获取iframe中的元素,但其实很简单就可以做到的,就是一级一级获取就可以了,获取至你需要的那个层级即可,下面看下实际的案例:(转) <fra ...

  10. 深入浅出Spring(二)

    IoC概念 控制反转(Inversion of Control)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题. 它还有一个名字叫做依赖注入(Dependency Injection).Io ...