题目传送门

题意:输出两字符串的最长公共子序列长度

分析:LCS(Longest Common Subsequence)裸题。状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; (s[i] == t[i])dp[i+1][j+1] = max (dp[i][j+1], dp[i+1][j]); (s[i] != t[i])

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std; const int MAXN = 3e2 + 10;
const int INF = 0x3f3f3f3f; char s[MAXN];
char t[MAXN];
int dp[MAXN][MAXN]; void LCS(int len1, int len2)
{
for (int i=0; i<len1; ++i)
{
for (int j=0; j<len2; ++j)
{
if (s[i] == t[j])
{
dp[i+1][j+1] = dp[i][j] + 1;
}
else
{
dp[i+1][j+1] = max (dp[i][j+1], dp[i+1][j]);
}
}
}
} int main(void) //POJ 1458 Common Subsequence
{
#ifndef ONLINE_JUDGE
freopen ("LCS.in", "r", stdin);
#endif while (~scanf ("%s%s", &s, &t))
{
memset (dp, 0, sizeof (dp));
int len1 = strlen (s);
int len2 = strlen (t); LCS (len1, len2);
printf ("%d\n", dp[len1][len2]);
} return 0;
}

  

LCS POJ 1458 Common Subsequence的更多相关文章

  1. POJ 1458 Common Subsequence(LCS最长公共子序列)

    POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...

  2. POJ 1458 Common Subsequence(最长公共子序列LCS)

    POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...

  3. Poj 1458 Common Subsequence(LCS)

    一.Description A subsequence of a given sequence is the given sequence with some elements (possible n ...

  4. (线性dp,LCS) POJ 1458 Common Subsequence

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65333   Accepted: 27 ...

  5. POJ - 1458 Common Subsequence DP最长公共子序列(LCS)

    Common Subsequence A subsequence of a given sequence is the given sequence with some elements (possi ...

  6. poj 1458 Common Subsequence【LCS】

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43132   Accepted: 17 ...

  7. OpenJudge/Poj 1458 Common Subsequence

    1.链接地址: http://poj.org/problem?id=1458 http://bailian.openjudge.cn/practice/1458/ 2.题目: Common Subse ...

  8. POJ 1458 Common Subsequence (动态规划)

    题目传送门 POJ 1458 Description A subsequence of a given sequence is the given sequence with some element ...

  9. poj 1458 Common Subsequence

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 46387   Accepted: 19 ...

随机推荐

  1. IIS7.0配置网站时,提示“ISAPI 和 CGI 限制”

    把网站配置到IIS上的时候,访问网站提示如下错误:  

  2. .Net如何在后台设置日期格式,并显示在前台页面上

    其实方法比较老咯,有比这个简单的朋友请留言哈,我的思路是先将数据库中的日期格式读出来,在后台转化成DatetTime类型,然后在使用DateTime的内部方法设置日期格式,代码如下: DateTime ...

  3. Spring面向切面编程(AOP)方式二

    使用注解进行实现:减少xml文件的配置. 1 建立切面类 不需要实现任何特定接口,按照需要自己定义通知. package org.guangsoft.utils; import java.util.D ...

  4. JavaEE编码题

    1.请编写代码实现登录效果(5分) 要求: 1)手写出相应的HTML和CSS代码 2)字体大小12px,表格宽300px,按钮行占两列并水平居中, 3)可以写在style节点内,也可使用行内CSS或者 ...

  5. iOS中UITableView的一些设置

    不可滑动: ? 1 tableView.userInteractionEnabled = NO; 也可以在storyboard中的userInteractionEnable属性设置 显示导向箭头: ? ...

  6. 3ds max删除了对象后,还是将原来所有对象输出的原因

    原因是场景中除了 几何体 外还有 图形,如下图 将这些图形删除,几何体就都正常输出了.

  7. 双栈排序(codevs 1170)

    题目描述 Description Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈 ...

  8. vs2013中项目依赖项的作用

    依赖项就是设定项目所以来的项目,以决定具体生成解决方案时,项目编译的顺序(一般一个解决方案会有很多项目组成). 通常来说,依赖项取决于这个项目引用的组件和项目,系统可以自己决定. 作用就是让系统知道你 ...

  9. DB2 bind on z/os

    BIND and REBIND options for packages and plans There are several options you can use for binding or ...

  10. Python 打包程序判断是否已经运行

    代码如下: # -*- coding: UTF8 -*- from win32com.client import Dispatch import win32com import sys, os fro ...