Common Subsequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 46630   Accepted: 19154

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.

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

Java AC 代码:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
String first = "";
String second = "";
while(!(first = sc.next()).equals("") && !(second = sc.next()).equals("")) {
char[] firstArray = first.toCharArray();
char[] secondArray = second.toCharArray();
int firstLen = first.length();
int secondLen = second.length();
int[][] subMaxLen = new int[firstLen + 1][secondLen + 1]; //这里设置成长度加1的,是为了防止下面 i-1 j-1的时候数组越界。
for(int i = 1; i <= firstLen; i++)
for(int j = 1; j <= secondLen; j++) {
if(firstArray[i - 1] == secondArray[j - 1])
subMaxLen[i][j] = subMaxLen[i - 1][j - 1] + 1;
else
subMaxLen[i][j] = (subMaxLen[i - 1][j] > subMaxLen[i][j - 1] ? subMaxLen[i - 1][j] : subMaxLen[i][j - 1]);
}
System.out.println(subMaxLen[firstLen][secondLen]);
} } }

poj 1458 Common Subsequence(dp)的更多相关文章

  1. POJ 1458 Common Subsequence (DP+LCS,最长公共子序列)

    题意:给定两个字符串,让你找出它们之间最长公共子序列(LCS)的长度. 析:很明显是个DP,就是LCS,一点都没变.设两个序列分别为,A1,A2,...和B1,B2..,d(i, j)表示两个字符串L ...

  2. POJ 1458 Common Subsequence(LCS最长公共子序列)

    POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...

  3. POJ 1458 Common Subsequence (动态规划)

    题目传送门 POJ 1458 Description A subsequence of a given sequence is the given sequence with some element ...

  4. POJ 1458 Common Subsequence(最长公共子序列)

    题目链接Time Limit: 1000MS Memory Limit: 10000K Total Submissions: Accepted: Description A subsequence o ...

  5. poj 1458 Common Subsequence(区间dp)

    题目链接:http://poj.org/problem?id=1458 思路分析:经典的最长公共子序列问题(longest-common-subsequence proble),使用动态规划解题. 1 ...

  6. poj 1458 Common Subsequence ——(LCS)

    虽然以前可能接触过最长公共子序列,但是正规的写应该还是第一次吧. 直接贴代码就好了吧: #include <stdio.h> #include <algorithm> #inc ...

  7. LCS POJ 1458 Common Subsequence

    题目传送门 题意:输出两字符串的最长公共子序列长度 分析:LCS(Longest Common Subsequence)裸题.状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; ( ...

  8. (线性dp,LCS) POJ 1458 Common Subsequence

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65333   Accepted: 27 ...

  9. POJ - 1458 Common Subsequence DP最长公共子序列(LCS)

    Common Subsequence A subsequence of a given sequence is the given sequence with some elements (possi ...

随机推荐

  1. Jenkins初次启动卡住问题解决

    Jenkins在初次使用时, 一直卡住, 无论如何也不出现输入用户名密码. 忘记截图, 下次出现更新图示. 解决方案: 需要你进入Jenkins的工作目录,打开 hudson.model.Update ...

  2. 几行python代码解决相关词联想

    日常生活中经常会遇到相关词联想的问题,也就是说输入一个词汇,把相关的词汇查询出来,听起来这个做法也不是太难,但如何去积累那么多的词汇,再用好的算法将相关内容联系起来,本身还是不简单的.笔者认为最简单的 ...

  3. httpClient调用接口的时候,解析返回报文内容

    比如我httpclient调用的接口返回的格式是这样的: 一:data里是个对象 { "code": 200, "message": "执行成功&qu ...

  4. "首页添加至购物车,TabBar显示购物车的数量"实现

    今天学习别人的项目源码的时候,看到这样的一种实现功能:首页添加至购物车,TabBar显示购物车的数量....想到以前没有做过,这里学习了,记录一下: 实现的效果图如下: 当点击首页添加至购物的操作的时 ...

  5. [转] Maven更新父子模块的版本号, mvn versions:set

    [From]https://www.cnblogs.com/ilovexiao/p/5663761.html 前置条件: 1.安装有吃饭的家伙JAVA和MAVEN. 首先,需要有一个packaging ...

  6. BaseDao+万能方法 , HibernateDaoSupport

    package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStat ...

  7. random、json、pickle、hashlib、hmac、shutil、shevle模块

    今日内容: 1. random 模块 2. json模块 3. pickle 模块 4.hashlib 模块 5. hmac 模块 6. shutil 模块 7. shelve 模块 1. rando ...

  8. jvm的学习笔记:二、类的初始化,代码实战(2)

    常量在编译阶段,会存在调用这个常量的方法的所在的类的常量池当中 System.out.println(MyParent2.str); 输出: hello parent2 依据:在MyTest2类调用M ...

  9. 【AMAD】transitions -- 一个python实现的轻量级,面向对象的有限状态机

    简介 个人评分 简介 Transitions1是使用python实现的有限状态机2. 而有限状态机是实现经典模式 -- 状态模式3的前提. 这个库的API相当优雅,简洁. 另外博客园有人发布博客4介绍 ...

  10. js ajax跨域被阻止 CORS 头缺少 'Access-Control-Allow-Origin'(转)

    今天ajax请求域名的时候出现 已阻止跨源请求:同源策略禁止读取位于 http://www.zuimeimami.com*****的远程资源.(原因:CORS 头缺少 'Access-Control- ...