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. AES 加解密

    // // NSData+AES.h // Smile // // Created by 蒲晓涛 on 12-11-24. // Copyright (c) 2012年 BOX. All rights ...

  2. ListView使用自定义适配器的情况下实现适配器的控件点击事件执行Activity界面中的方法

    如果ListView使用的是自定义的适配器,比如MyArrayAdapter extends ArrayAdapter<String> 那么,如何实现适配器中的点击事件执行activity ...

  3. Ehcache(2.9.x) - API Developer Guide, Class Loading

    About Class Loading Class loading, within the plethora of environments that Ehcache can be running, ...

  4. Windows Kernel Way 扉言

    七年寒窗,但求一道. 笔者在学习windows/linux以及各类编程语言.框架之初因摸不到门路而磕磕绊绊,因寻不到明师而步履蹒跚,或不知缘从何起,或不知路在何处,只能尝试.回溯.重来.反反复复,竟也 ...

  5. C# to Maxscript

    I figured I’d do a quick tutorial about something a little more difficult, but still very important. ...

  6. OC7_目录操作

    // // main.m // OC7_目录操作 // // Created by zhangxueming on 15/6/19. // Copyright (c) 2015年 zhangxuemi ...

  7. (转)使用Visual Studio 2015开发Android 程序

    环境配置: 操作系统:win 7 64位 IDE:Visual Studio 2015 SDK:installer_r24.3.3-windows 安装前提: 编辑hosts文件(在附件可下载)因为安 ...

  8. (转)C# 数据类型映射 (SQLite,MySQL,MSSQL,Oracle)

    一.C# vs SQLite: C# SQLite 字段名 类型 库类型 GetFieldType(#) 转换 备注 F_BOOL bool BIT NOT NULL Boolean F_BOOL_N ...

  9. Javascript中的函数

    Javascript中的函数 1.什么是函数 函数是被命名的,独立的,完成特定功能的代码段.其可能给调用它的程序返回值,我们把这个代码段就称之为"函数". 被命名的:函数大部分都是 ...

  10. java匿名内部类练习

    interface Inter { void method(); } class Test { //补足代码.通过匿名内部类. /* static class Inner implements Int ...