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 ...
随机推荐
- 【BZOJ1006】【HNOI2008】神奇的国度(弦图染色)
1006: [HNOI2008]神奇的国度 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1467 Solved: 603[Submit][Stat ...
- iOS -- 给model赋值时走了[self setValuesForKeysWithDictionary:dic]不走setvalue: forked:
这是一个小坑, 看看你的BaseModel的便利构造器的方法: + (__kindof BaseModel *)modelWithDic:(NSDictionary *)dic { return [[ ...
- 微信WeixinJSBridge API
<!DOCTYPE html><html> <head> <title>微信WeixinJSBridge API</title> <m ...
- 详解在visual studio中使用git版本系统(图文)
很多人已经在使用git(或正在转移到git上),在github.com上,也看到园子里不少同学的开源项目,非常不错.但相关教程似乎不多,所以趁着我自己的开源项目源码托管(https://github. ...
- 【CodeForces 489A】SwapSort
题 Description In this problem your goal is to sort an array consisting of n integers in at most n sw ...
- com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'db_item'
一直很奇怪,为什么报错,进入mysql命令行,show databases:发现多谢了一个空格,如上图.
- MyEclipse------File类的各种方法
usingFile.jsp <%@ page language="java" import="java.util.*" pageEncoding=&quo ...
- linux建立用户 详细
.你同时属于两个或两个以上的组. 两个条件你至少具备一个,你才能够把文件所属旧组变为新组.使用如下的命令将当前目录下所有html文件所属的组改为httpd: chgrp httpd *.html 和c ...
- 改变了Tomcat路径后无法卸载和重装的解决办法
错误如下: ---------------------------Apache Tomcat Setup---------------------------Failed to install Tom ...
- Nginx 服务器性能参数设置
Nginx服务器性能调优 Nginx 配置文件 1.根据CPU内核数设置worker进程个数,以12核CPU为例,设置11个worker进程: worker_processes 11; worker_ ...