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, 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 题意:求最大公共子序列的长度 解题思路:
 用d[i][j]表示公共子序列的长度。
      如果x[i-1]==y[j-1]
           d[i][j]=d[i-1][j-1]+1
      否则
           d[i][j]=max(d[i-1][j],d[i][j-1])
       代码如下:
 #include <stdio.h>
#include <string.h>
int max(int a,int b)
{
return a>b?a:b;
}
char x[],y[];
int d[][];
int main()
{
while(scanf("%s%s",&x,&y)!=EOF)
{
//memset(d,0,sizeof(d));
int lenx=strlen(x),leny=strlen(y);
for(int i=; i<=lenx; i++)
{
for(int j=; j<=leny; j++)
{
if(x[i-]==y[j-])
{
d[i][j]=d[i-][j-]+;
//printf("x[%d]=%c y[%d]=%c d[%d][%d]=%d %d\n",i-1,x[i-1],j-1,y[j-1],i-1,j-1,d[i-1][j-1],d[i][j]);
}
else
{
d[i][j]=max(d[i-][j],d[i][j-]);
// printf("x[%d]=%c y[%d]=%c d[%d][%d]=%d d[%d][%d]=%d %d\n",i-1,x[i-1],j-1,y[j-1],i-1,j,d[i-1][j],i,j-1,d[i][j-1],d[i][j]);
} }
}
printf("%d\n",d[lenx][leny]);
}
}

    

HDU 1159的更多相关文章

  1. HDU 1159 Common Subsequence

    HDU 1159 题目大意:给定两个字符串,求他们的最长公共子序列的长度 解题思路:设字符串 a = "a0,a1,a2,a3...am-1"(长度为m), b = "b ...

  2. hdu 1159 Palindrome(回文串) 动态规划

    题意:输入一个字符串,至少插入几个字符可以变成回文串(左右对称的字符串) 分析:f[x][y]代表x与y个字符间至少插入f[x][y]个字符可以变成回文串,可以利用动态规划的思想,求解 状态转化方程: ...

  3. HDU 1159 Common Subsequence 最长公共子序列

    HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...

  4. HDU 1159 Common Subsequence 公共子序列 DP 水题重温

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  5. HDU 1159 Common Subsequence:LCS(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 题意: 求最长公共子序列. 题解: (LCS模板题) 表示状态: dp[i][j] = max ...

  6. HDU 1159 Common Subsequence【dp+最长公共子序列】

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. hdu 1159 Common Subsequence(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  8. hdu 1159 Common Subsequence(最长公共子序列 DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  9. hdu 1159 Common Subsequence 【LCS 基础入门】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1159 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  10. HDU 1159 Common Subsequence(裸LCS)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

随机推荐

  1. css笔记15:盒子模型

    1.流 流:html元素在网页中显示的顺序 标准流:在html文件中,写在前面的元素在前面显示,写在后面的html元素在后面显示. 非标准流:在html之中,当某个元素脱离了标准流,那么它处于非标准流 ...

  2. [课程相关]homework-08

    一.变量作用域和生命周期 #include <cstdlib> #include <iostream> using namespace std; void try_change ...

  3. 利用OPENSSL 实现MD5加密。

    #include <stdio.h> #include "openssl/evp.h" #include "openssl/md5.h" #incl ...

  4. fancybox的使用

    fancybox,个人没有深入了解,只是为了工作需要,做的一些界面,主要是用的AJAX功能. 首先,需要下载fancybox的js文件以及CSS文件(可能用不到) 其次,在页面中引入 <scri ...

  5. jsp无法支持el标签及jstl标签

    在jsp页面头部添加如下 <%@ page isELIgnored="false"%> <%@ taglib uri="http://java.sun. ...

  6. 基于BaseHTTPServer的简单存储服务器

    服务器代码: from BaseHTTPServer import BaseHTTPRequestHandler from BaseHTTPServer import HTTPServer impor ...

  7. Oracle数据库数据同步方案

    一.比较原始的方案:触发器/Job/快照+dblink的方式,可实现同步和定时刷新: 二台不同的数据库服务器,从一台数据库服务器A的一个用户读取另一台数据库服务器B下某个用户的数据,可以通过dblin ...

  8. 转:java服务器安全指南

    转: http://drops.wooyun.org/web/16609 JAVA安全之JAVA服务器安全漫谈 z_zz_zzz · 2016/06/08 10:50 0x00 前言 本文主要针对JA ...

  9. 【原创】 windows下开发软件推荐

    1. 数据库查看器工具 navicat.exe

  10. MyBatis(3.2.3) - Dynamic SQL

    Sometimes, static SQL queries may not be sufficient for application requirements. We may have to bui ...