HDU-1159 Common Subsequence 最长上升子序列
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9595 Accepted Submission(s): 3923
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. 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.

1 #include <cstring>
2 #include <cstdlib>
3 #include <cstdio>
4 #define Max( a, b ) (a) > (b) ? (a) : (b)
5 using namespace std;
6
7 char s1[1005], s2[1005];
8
9 int dp[1005][1005];
10
11 int main()
12 {
13 int len1, len2;
14 while( scanf( "%s %s", s1, s2 ) != EOF )
15 {
16 memset( dp, 0, sizeof(dp) );
17 len1 = strlen( s1 ), len2 = strlen( s2 );
18 for( int i = 1; i <= len1; ++i )
19 {
20 for( int j = 1; j <= len2; ++j )
21 {
22 if( s1[i-1] == s2[j-1] )
23 {
24 dp[i][j] = dp[i-1][j-1] + 1;
25 }
26 else
27 {
28 dp[i][j] = Max ( dp[i-1][j], dp[i][j-1] );
29 }
30 }
31 }
32 printf( "%d\n", dp[len1][len2] );
33 }
34 return 0;
35 }

第二种处理方法:
#include <cstring>
#include <cstdlib>
#include <cstdio>
#define Max( a, b ) (a) > (b) ? (a) : (b)
using namespace std;
char s1[1005], s2[1005];
int dp[1005][1005];
int main()
{
int len1, len2;
while( scanf( "%s %s", s1+1, s2+1 ) != EOF )
{
memset( dp, 0, sizeof(dp) );
len1 = strlen( s1+1 ), len2 = strlen( s2+1 );
for( int i = 1; i <= len1; ++i )
{
for( int j = 1; j <= len2; ++j )
{
if( s1[i] == s2[j] )
{
dp[i][j] = dp[i-1][j-1] + 1;
}
else
{
dp[i][j] = Max ( dp[i-1][j], dp[i][j-1] );
}
}
}
printf( "%d\n", dp[len1][len2] );
}
HDU-1159 Common Subsequence 最长上升子序列的更多相关文章
- HDU 1159 Common Subsequence 最长公共子序列
HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...
- hdu 1159 Common Subsequence(最长公共子序列 DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...
- HDU 1159 Common Subsequence
HDU 1159 题目大意:给定两个字符串,求他们的最长公共子序列的长度 解题思路:设字符串 a = "a0,a1,a2,a3...am-1"(长度为m), b = "b ...
- C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解
版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...
- POJ 1458 Common Subsequence(最长公共子序列LCS)
POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...
- lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)
Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength ...
- hdu 1159 Common Subsequence(最长公共子序列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...
- HDU 1159 Common Subsequence 公共子序列 DP 水题重温
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...
- HDU 1159 Common Subsequence(裸LCS)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...
- HDU 1159 Common Subsequence【dp+最长公共子序列】
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- sql脚本比较大,sqlserver 无法导入,就用cmd命令执行
osql简单用法:用来将本地脚本执行,适合sql脚本比较大点的情况,执行起来比较方便 1 osql -S serverIP -U sa -P 123 -i C:\script.sql serverIP ...
- cygwin 的不同文件类型显示不同的颜色
正常情况下,我们的cygwin安装好之后,不管是文件还是文件夹显示的颜色都是一样的,这样在我们平时的工作中很不方便 所以这里记录一下怎么设置成彩色的,其实很简单. 直接修改文件 .bashrc vim ...
- bootstrap_UI
- 【ZOJ 3480】Duck Typing
题 题意 1.有t组数据,输入时每组数据之间空格隔开,输出时也要求空格隔开. 2.每组都是一行begin开始,一行end结束. 3.class ClassName[:Super] 表示声明一个类型,S ...
- 洛谷P2726 阶乘 Factorials
题目背景 N的阶乘写作N!,表示小于等于N的所有正整数的乘积. 题目描述 阶乘会变大得很快,如13!就必须用32位整数类型来存储,到了70!即使用浮点数也存不下了. 你的任务是找到阶乘最前面的非零位. ...
- javascript-如何判断一个对象为数组
Q:如何判断一个对象是否为数组? A1:判断对象的constructor是否指向Array, 接着判断对应的特殊属性,如length,splice之类.这个很容易冒充. A2:使用instanceof ...
- The Dirichlet Distribution 狄利克雷分布 (PRML 2.2.1)
The Dirichlet Distribution 狄利克雷分布 (PRML 2.2.1) Dirichlet分布可以看做是分布之上的分布.如何理解这句话,我们可以先举个例子:假设我们有一个骰子,其 ...
- WinsockExpert+NC抓包上传之拿WEBSHELL
知识补充: nc上传漏洞在原理上同动网上传漏洞一样,都是利用计算机在读取字符串时,遇到'\0'(00)时,认为字符串结束了,从而丢掉后面的字符串,正如unicode编码特性一样,可被人利用,尽管在这里 ...
- 利用ps橡皮擦工具快速抠图
原图 最终效果 1.打开图片,ctrl+j得到图层1,点击红圈处,创建图层2,放于图层1与背景层之间,填充白色作为检查效果和新的背景. 2.按图示给出的参数,用背景橡皮擦在图层1里擦吧,注意擦的时候尽 ...
- 对称加密和分组加密中的四种模式(ECB、CBC、CFB、OFB)
一. AES对称加密: AES加密 分组 二. 分组密码的填充 分组密码的填充 e.g.: PKCS#5填充方式 三. 流密码: 四. 分组密码加密中的四种模式: 3.1 ECB模式 优点: 1. ...