一、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.



二、题解

        这题和之前做的poj 1159 的解法相同,都是用到了动态规划解LCS算法。详情请看1159.本来这道题应该很容易就解出来的,但是由于BBS上的数据错误,害我以为不是LCS,但是这个题目的定义就是LCS的形式化定义,所以纠结了一阵。后来发现原来是数据的错误。

三、Java代码

    import java.util.Scanner;   

    public class Main {
public static int LCS(String x,String y){
short [][] z=new short [x.length()+1][y.length()+1];
short i,j;
for( i=0;i<=x.length();i++)
z[i][0]=0;
for( j=0;j<=y.length();j++)
z[0][j]=0; for(i=1;i<=x.length();i++){
for( j=1;j<=y.length();j++){
if(x.charAt(i-1)==y.charAt(j-1)){
z[i][j]= (short) (z[i-1][j-1]+1);
}
else
z[i][j]=z[i-1][j] > z[i][j-1] ?z[i-1][j]:z[i][j-1];
}
}
return z[x.length()][y.length()];
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while(cin.hasNext()){
System.out.println(LCS(cin.next(),cin.next()));
}
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Poj 1458 Common Subsequence(LCS)的更多相关文章

  1. LCS POJ 1458 Common Subsequence

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

  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(最长公共子序列LCS)

    POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...

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

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

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

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

  6. poj 1458 Common Subsequence【LCS】

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43132   Accepted: 17 ...

  7. OpenJudge/Poj 1458 Common Subsequence

    1.链接地址: http://poj.org/problem?id=1458 http://bailian.openjudge.cn/practice/1458/ 2.题目: Common Subse ...

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

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

  9. poj 1458 Common Subsequence

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 46387   Accepted: 19 ...

随机推荐

  1. POJ 2856 Y2K Accounting Bug【简单暴力】

    链接: http://poj.org/problem?id=2586 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26733#probl ...

  2. NSURLSession各文件关系

    NSURLSession   通过session创建任务 @property (class, readonly, strong) NSURLSession *sharedSession; + (NSU ...

  3. python基础-第九篇-9.2线程与多线程

    单线程 import time beginTime = time.time() for a in range(10): print(a) time.sleep(1) shijian = time.ti ...

  4. Django redis2 列表 和其他操作

    列表的操作 List操作,redis中的List在在内存中按照一个name对应一个List来存储.如图: lpush插值至列表最左边 lpush(name,values) # 在name对应的list ...

  5. xutils3文件上传、下载、get、post请求

    @ContentView(R.layout.activity_xutils3_net) public class XUtils3NetActivity extends Activity { @View ...

  6. 简介windows的环境变量

    环境变量一般是指在操作系统中用来指定操作系统运行环境的一些参数,比如临时文件夹位置和系统文件夹位置等.这点有点类似于DOS时期的默认路径,当你运行某些程序时除了在当前文件夹中寻找外,还会到设置的默认路 ...

  7. c#命名规则参考

    命名规则参考:1.从组件类型名中移去T前缀.例如TButton变成Button.2.除了第一个元音,删去所有元音字母.例如,Button变成bttn,Edit变成edt.3.压缩双字母.例如,bttn ...

  8. Render树、RenderObject与RenderLayer

    Chapter: 呈现树的构建 1. 呈现树与CSS盒子模型千丝万缕的关系 2. 呈现树与DOM树的关系 3. 浏览器构建呈现树的流程 4. Firefox的规则树和样式上下文树 5. 规则树是如何解 ...

  9. Linux查看端口占用进程

    Linux查看端口占用进程 netstat -anlp|grep 8081 tcp /java 此处3195为进程ID

  10. Future模式实例

    Future模式 /** * qccr.com Inc. * Copyright (c) 2014-2016 All Rights Reserved. */ package com.youqianhu ...