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. Volley源码分析

    取消请求的源码分析: public void cancelAll(RequestFilter filter) { synchronized (mCurrentRequests) { for (Requ ...

  2. Pytorch-创建tensor

    引言 本篇介绍创建tensor的几种方式 Import from numpy from_numpy() float64 是 double 类型,也就是说从numpy导入的float其实是double类 ...

  3. iOS使用UIBezierPath实现ProgressView

    实现效果如下: 界面采用UITableView和TabelViewCell的实现,红色的视图采用UIBezierPath绘制.注意红色的部分左上角,左下角是直角哟!!!!不多说<这里才是用UIB ...

  4. Jmeter使用CSV Data参数化,中文参数传递过程出现乱码问题

    解决方式:文件编码改为GB2312.GBK.GB18030(utf-8同样会乱码)

  5. 【ABAP系列】SAP 使用事务码DBCO实现SAP链接外部数据库以及读取例程

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 使用事务码DBCO实现S ...

  6. Django 邮箱找回密码!!!!!!!!!!!!!!!!

    1.大概流程. @首先在完善登陆页面,增加忘记密码的链接. @为了账户安全,需要对操作者进行验证,向邮箱发随机数验证! @在重置验证码页面,验证验证码是否匹配(验证成功跳转至更改密码也页面). @ 重 ...

  7. kean的博客今天开通了,happy 一下

    希望以后可以日日勤勉,孜孜不倦的记录我的一生!

  8. python高级篇

    1.切片功能:类似于java中的split方法.对list或者triple中几个值进行取出的过程. L = ['a','b','c','d']   L[0:3] = ['a','b','c']   # ...

  9. @ComponentScan 注解

    在配置类上添加 @ComponentScan 注解.该注解默认会扫描该类所在的包下所有的配置类,相当于xml的 <context:component-scan>. @ComponentSc ...

  10. 强化学习应用于游戏Tic-Tac-Toe

    Tic-Tac-Toe游戏为3*3格子里轮流下棋,一方先有3子成直线的为赢家. 参考代码如下,我只删除了几个没用的地方: ####################################### ...