POJ - 1458 Common Subsequence DP最长公共子序列(LCS)
Common Subsequence
Input
Output
Sample Input
abcfbc abfcab
programming contest
abcd mnp
Sample Output
4
2
0 题解出处:http://blog.csdn.net/a_eagle/article/details/7213236
题目大意:给出两个字符串,求两个字符串的最长公共字串。
思路:慢慢重心开始有贪心转向动态规划了,这题就是简单的动态规划题。以题目的第一组测试数据为例。abcfbc abfcab。

可以看出:
F[i][j]=F[i-1][j-1]+1;(a[i]==b[j])
F[i][j]=max(F[i-1][j],F[i][j-1])(a[i]!=b[j]);
#include<stdio.h>
#include<string.h> int f[][];
char s1[],s2[]; int max(int x,int y)
{
return x>y?x:y;
} int main()
{
int n,i,j;
while(~scanf("%s %s",s1,s2)){
memset(f,,sizeof(f));
//int maxx=0;
for(i=;i<=strlen(s1);i++){
for(j=;j<=strlen(s2);j++){
if(s1[i-]==s2[j-]){
f[i][j]=f[i-][j-]+;
//if(f[i][j]>maxx) maxx=f[i][j];
}
//不加else
else f[i][j]=max(f[i-][j],f[i][j-]);
}
}
//printf("%d\n",maxx);
printf("%d\n",f[strlen(s1)][strlen(s2)]);
}
return ;
}
POJ - 1458 Common Subsequence DP最长公共子序列(LCS)的更多相关文章
- POJ 1458 Common Subsequence 【最长公共子序列】
解题思路:先注意到序列和串的区别,序列不需要连续,而串是需要连续的,先由样例abcfbc abfcab画一个表格分析,用dp[i][j]储存当比较到s1[i],s2[j]时最长公共子序 ...
- POJ 1458 Common Subsequence(最长公共子序列)
题目链接Time Limit: 1000MS Memory Limit: 10000K Total Submissions: Accepted: Description A subsequence o ...
- POJ1458 Common Subsequence —— DP 最长公共子序列(LCS)
题目链接:http://poj.org/problem?id=1458 Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Tot ...
- 题解报告:hdu 1159 Common Subsequence(最长公共子序列LCS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Problem Description 给定序列的子序列是给定的序列,其中有一些元素(可能没有) ...
- hdu 1159 Common Subsequence(最长公共子序列,DP)
题意: 两个字符串,判断最长公共子序列的长度. 思路: 直接看代码,,注意边界处理 代码: char s1[505], s2[505]; int dp[505][505]; int main(){ w ...
- hdu 1159 Common Subsequence (最长公共子序列 +代码)
Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...
- hdu 1159 Common Subsequence(最长公共子序列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...
- POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 56150 Accepted: 19398 Desc ...
- HDU 1159 Common Subsequence 【最长公共子序列】模板题
题目链接:https://vjudge.net/contest/124428#problem/A 题目大意:给出两个字符串,求其最长公共子序列的长度. 最长公共子序列算法详解:https://blog ...
随机推荐
- svn服务的安装和使用
更新linux软件库 cat /etc/redhat-release wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.c ...
- Linux搭建FTP服务器实战
首先准备一台Linux系统机器(虚拟机也可), 检测出是否安装了vsftpd软件: rpm -qa |grep vsftpd 如果没有输出结果,就是没有安装. 使用命令安装,安装过程中会有提示,直接输 ...
- Java for LeetCode 096 Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- Numpy数组的保存与读取方法
1. 数组以二进制格式保存 np.save和np.load是读写磁盘数组数据的两个主要函数.默认情况下,数组以未压缩的原始二进制格式保存在扩展名为npy的文件中,以数组a为例 np.save(&quo ...
- Machine Learning No.2: Linear Regression with Multiple Variables
1. notation: n = number of features x(i) = input (features) of ith training example = value of feat ...
- github 博客模板
http://www.jianshu.com/p/d658ba3b4351 http://jekyllthemes.org/
- MSSQL2005外网IP的1433端口开启方法
打开SQL Server Configuration Manager,在SQL server配置管理器展开SQL server 2005网络配置-->SQLEXPRESS 的协议-->双击 ...
- Codeforces 854B Maxim Buys an Apartment:贪心
题目链接:http://codeforces.com/contest/854/problem/B 题意: 有n栋房子从1到n排成一排,有k栋房子已经被售出. 现在你要买一栋“好房子”. 一栋房子是“好 ...
- github添加ssh公钥
使用git作为版本维护工具非常方便,而且一般个人用github作为远端库就够用了.而一般git连接github的方式采用ssh的方法,http的会略微慢一些.所以为了方便一般会在github设置中添加 ...
- python基础-元组
操作元组 获取元组中的值 tup1 = ('高数','计算机',2008,2016) tup2 = (1,2,3,4,5,6,7) #和list的一样,同样取下标1,2,3,4的值 print(&qu ...