hdoj 1159最长公共子序列
/*Common Subsequence
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, xij = 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*/
<span style="font-size:18px;">#include <stdio.h>
#include <string.h>
#define maxn 1000
char str1[maxn], str2[maxn];
int dp[maxn][maxn];
int max(int a, int b)
{
return a > b ? a : b;
}
int gg()
{
int m= 0;
for(int i = 1; str1[i]; ++i){
for(int j = 1; str2[j]; ++j){
if(str1[i] == str2[j]){
dp[i][j] = dp[i-1][j-1] + 1;
}else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
if(dp[i][j]>m) m= dp[i][j];
}
}
return m;
} int main()
{
while(scanf("%s%s", str1 + 1, str2 + 1)== 2){
printf("%d\n", gg());
}
return 0;
}
</span>
hdoj 1159最长公共子序列的更多相关文章
- HDU 1159 最长公共子序列(n*m)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1159 最长公共子序列
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- POJ 1159 Palindrome-最长公共子序列问题+滚动数组(dp数组的重复利用)(结合奇偶性)
Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...
- HDU 1159 Common Subsequence:LCS(最长公共子序列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 题意: 求最长公共子序列. 题解: (LCS模板题) 表示状态: dp[i][j] = max ...
- POJ 1159 Palindrome(最长公共子序列)
http://poj.org/problem?id=1159 题意: 给出一个字符串,计算最少要插入多少个字符可以使得该串变为回文串. 思路: 计算出最长公共子序列,再用原长-LCS=所要添加的字符数 ...
- 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 最长公共子序列
HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...
- HDU 1159 Common Subsequence --- DP入门之最长公共子序列
题目链接 基础的最长公共子序列 #include <bits/stdc++.h> using namespace std; ; char c[maxn],d[maxn]; int dp[m ...
随机推荐
- hdu 1535 Invitation Cards (最短路径)
Invitation Cards Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 【POJ3415】 Common Substrings (SA+单调栈)
这道是求长度不小于 k 的公共子串的个数...很不幸,我又TLE了... 解法参考论文以及下面的链接 http://www.cnblogs.com/vongang/archive/2012/11/20 ...
- 2013年 ACMICPC 杭州赛区H题
思路:树状数组统计.待验证,不知道是否对. #include<cstdio> #include<cstring> #include<cmath> #include& ...
- Nodejs 基础知识 浅析
1. 模块化 ①常用模块化规范 CommonJS + nodejs AMD(Asynchronous Module Definition) + RequireJS CMD(Common Module ...
- iframe操作(跨域解决等)
note:当页面内嵌入一个iframe实际上是在dom上新建了一个新的完整的window对象 iframe中取得主窗体 window.top (顶级窗口的window对象) window.parent ...
- 汕头市队赛 SRM13 T2
这道题很容易想到是二分 但是因为可能会爆LL 所以要加一波特判 #include<cstdio> #include<cstring> #include<algorithm ...
- 【查看linux配置】查看linux系统常用的命令,Linux查看系统配置常用命令
一.linux CPU大小 cat /proc/cpuinfo |grep "model name" && cat /proc/cpuinfo |grep &qu ...
- 魔法使的烟花(NOIP模拟赛Round 7)
[问题描述] 魔法森林里有很多蘑菇,魔法使常常采摘它们来制作魔法药水.为了在6月的那个奇妙的晚上用魔法绽放出最绚丽的烟花,魔法使决定对魔法森林进行一番彻底的勘探. 魔法森林分为n个区域,由n-1条长度 ...
- android与java的关系
摘自:http://bbs.51cto.com/thread-944897-1.html 相信学习android的人都会想过或者想知道这个问题,那就请你耐心的看完这篇文章吧,你会对android与 ...
- Appium+python自动化12-appium元素定位【转载】
前言 appium定位app上的元素,可以通过id,name.class这些属性定位到 一.id定位 1.appium的id属性也就是通过UI Automator工具查看的resource-id属性