HDU 1159 Common Subsequence (LCS)
题意:给定两行字符串,求最长公共子序列。
析:dp[i][j] 表示第一串以 i 个结尾和第二个串以 j 个结尾,最长公共子序列,剩下的就简单了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 5;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int dp[maxn][maxn];
char s1[maxn], s2[maxn]; int main(){
while(scanf("%s %s", s1+1, s2+1) == 2){
n = strlen(s1+1);
m = strlen(s2+1);
memset(dp, 0, sizeof dp);
int ans = 0;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
if(s1[i] == s2[j]) { dp[i][j] = dp[i-1][j-1] + 1; ans = Max(ans, dp[i][j]); }
else { dp[i][j] = Max(dp[i-1][j], dp[i][j-1]); ans = Max(ans, dp[i][j]); } printf("%d\n", ans);
}
return 0;
}
HDU 1159 Common Subsequence (LCS)的更多相关文章
- hdu 1159 Common Subsequence(LCS最长公共子序列)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1159 Common Subsequence
HDU 1159 题目大意:给定两个字符串,求他们的最长公共子序列的长度 解题思路:设字符串 a = "a0,a1,a2,a3...am-1"(长度为m), b = "b ...
- HDU 1159 Common Subsequence 最长公共子序列
HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...
- 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(最长公共子序列)
题目链接: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(最长公共子序列 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 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- hdu 1159 Common Subsequence(LCS)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- MySQL添加数据库的唯一索引的几种方式~
创建表时直接设置: DROP TABLE IF EXISTS `student`;CREATE TABLE `student` ( `stu_id` int(11) NOT NULL AUTO_IN ...
- 福利向:几款给力的Unity脚本插件推荐
转自:http://www.gamelook.com.cn/2016/09/264877 Unity的Asset Store中除了拥有非常强大的Unity编辑器扩展工具之外,还有一些让开发过程事半功倍 ...
- C和指针 第三章--数据
简要概述: <C和指针>第三章对数据进行了描述. 其中主要讲解了---变量的三个属性:作用域.链接属性和存储类型. 这三个属性决定了该变量在“什么地方可以使用”以及“该变量的值能够保持多久 ...
- windows server 2008 配置DNS服务器与IIS
0x00: 总结这个星期在学校学的. 0x01安装: 首先你得安装好windows server 2008 然后在添加角色->安装IIS和DNS服务器 勾选好你要安装的. 安装-> 根据老 ...
- CentOS7.6安装JDK(Openjdk) - mvn package报错汇总
错误一: No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK ...
- SuggestFrameWork js代码结构
关于suggestFrameWork的使用教程网上很多,如果您仅仅想知道如何使用请移步.这里展现一下js代码实现结构 下载地址 http://sourceforge.net/projects/sugg ...
- 6. H.264/AVC编码器原理
1. H.264/AVC的应用 H.264 不仅具有优异的压缩性能,而且具有良好的网络亲和性,这对实时的视频通信是十分重要的.和 MPEG-4 中的重点是灵活性不同,H.264 着重在压缩的高效率和传 ...
- Java设计模式之——抽象工厂
抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂.该超级工厂又称为其他工厂的工厂.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 在抽 ...
- C++提高编译与链接速度的资料
1,https://blog.csdn.net/lihao21/article/details/47610309 2,https://www.zhihu.com/question/37330979 3 ...
- PHP - 闭合标签
最最开始的时候经常遇到这个问题,就是如果一个文件里面全部都是php代码的话,我写了前闭合和后闭合的时候,文件一多就容易报错,老是说什么有关输出的错误,貌似大概就是header已经发了. 手册上面这个样 ...