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 ...
随机推荐
- var foo = "11"+2+"1"; console.log(foo); //1121 好多文章答案写错了,我发下给初学的朋友看到,以免一开始就学错了
体会加一个字符串'1' 和 减去一个字符串'1'的不同 var foo = "11"+2-"1"; console.log(foo); //111 consol ...
- 使用AXIS2作为Client訪问WebService
使用AXIS2,能够方便的构建WebService的server端,也能够非常方便的作为Cilent,来訪问别的WebService. 以下依据工作中的经历,整理了一下,作为Cilent訪问WebSe ...
- opencv轮廓提取、轮廓识别相关要点
1.轮廓提取 src = cv2.imread("***.jpg", cv2.IMREAD_COLOR) gray = cv2.cvtColor(src ,cv2.COLOR_BG ...
- VIM中使用tab键自动完成(vim tab键自动补全 )插件supertab
supertab.vmb 这个插件好好用, Tab自动补全 http://www.vim.org/scripts/script.php?script_id=1643 安装步骤: 1.下载 supert ...
- Java for LeetCode 121 Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- vue应用示例
1 . 实现轮播图 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=&q ...
- vue --- axios , vuex
一 . 内容回顾 1.webpack(前端中工作,项目上线之前对整个前端项目优化) - entry:整个项目的程序入口(main.js或index.js): - output:输出的出口: - loa ...
- Python爬虫 —— 抓取美女图片
代码如下: #coding:utf-8 # import datetime import requests import os import sys from lxml import etree im ...
- POJ3450 Corporate Identity —— 后缀数组 最长公共子序列
题目链接:https://vjudge.net/problem/POJ-3450 Corporate Identity Time Limit: 3000MS Memory Limit: 65536 ...
- LightOJ - 1284 Lights inside 3D Grid —— 期望
题目链接:https://vjudge.net/problem/LightOJ-1284 1284 - Lights inside 3D Grid PDF (English) Statistic ...