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. github使用成长记

    学校里一直都有自己写一些网页,一方面为了学习熟练技能,另一方面也是兴趣所在.但是独乐乐不如众乐乐,一直向往有那么一个平台能把自己做得东西分享给广大网友,并且想借着分享的契机和各位程序猿交流学习心得(这 ...

  2. Objective-C ,ios,iphone开发基础:3分钟教你做一个iphone手机浏览器

    第一步:新建一个Single View工程: 第二步:新建好工程,关闭arc. 第三步:拖放一个Text Field 一个UIButton 和一个 UIWebView . Text Field 的ti ...

  3. java调用.net asmx服务

    有时候,在java下开发会调用一下.net下写的服务,看网上有各种方法,但总是不成功,总结下自己测试过能调用成功的方式: 1. 原始方式http-soap public static String p ...

  4. hdu 3473 裸的划分树

    思路: 用Sum[dep][i]记录从tree[po].l到i中进入左子树的和. #include<iostream> #include<algorithm> #include ...

  5. Ehcache(2.9.x) - API Developer Guide, Blocking and Self Populating Caches

    About Blocking and Self-Populating Caches The net.sf.ehcache.constructs package contains some applie ...

  6. fuse挂载hdfs目录到linux本地

    1,安装fuse yum -y install hadoop-hdfs-fuse 2.修改环境变量 vi /etc/profile 增加如下配置: JAVA_HOME=/usr/jdk64/jdk1. ...

  7. Agile.Net 组件式开发平台 - 系统文档中心

    Agile.Debgu.exe 文件为平台文档中心应用程序,该程序集成了数据库结构文档查询.数据库结构文档浏览.实时系统日志监控等功能.  数据库结构文档浏览 数据库结构文档查询 系统平台日志监控

  8. 第六十六篇、OC_Sqlite数据库操作

    #import <Foundation/Foundation.h> #import <sqlite3.h> #define kFilename @"data.sqli ...

  9. 委托和事件[delegate and event]_C#

    委托和事件: 1. 委托:一个能够表示方法的数据类型:它将方法作为对象封装起来,允许在运行时间接地绑定一个方法调用. 2. 声明委托数据类型: public delegate  bool Greate ...

  10. 2013山东省“浪潮杯”省赛 A.Rescue The Princess

    A.Rescue The PrincessDescription Several days ago, a beast caught a beautiful princess and the princ ...