(线性dp,LCS) POJ 1458 Common Subsequence
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 65333 | Accepted: 27331 |
Description
Input
Output
Sample Input
abcfbc abfcab
programming contest
abcd mnp
Sample Output
4
2
0 最长公共子序列问题(LCS) 其状态转换式为:A[i] = A[j]时,d(i,j) = d(i-1,j-1) + 1,否则d(i,j) = max{d(i-1,j),d(i,j-1)}
这个用char数组吧,用string可能出错,。。。打表
C++代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = ;
int dp[maxn][maxn];
char s1[maxn];
char s2[maxn];
int len1,len2;
int main(){
while(~scanf("%s%s",s1,s2)){
len1 = strlen(s1);
len2 = strlen(s2);
for(int i = ; i <= len1; i++){
dp[i][] = ;
}
for(int j = ; j <= len2; j++){
dp[][j] = ;
}
for(int i = ; i <= len1; i++){
for(int j = ; j <= len2; j++){
if(s1[i-] == s2[j-])
dp[i][j] = dp[i-][j-] + ;
else
dp[i][j] = max(dp[i][j-],dp[i-][j]);
}
}
printf("%d\n",dp[len1][len2]);
}
return ;
}
(线性dp,LCS) POJ 1458 Common Subsequence的更多相关文章
- LCS POJ 1458 Common Subsequence
题目传送门 题意:输出两字符串的最长公共子序列长度 分析:LCS(Longest Common Subsequence)裸题.状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; ( ...
- POJ 1458 Common Subsequence(LCS最长公共子序列)
POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...
- POJ - 1458 Common Subsequence DP最长公共子序列(LCS)
Common Subsequence A subsequence of a given sequence is the given sequence with some elements (possi ...
- POJ 1458 Common Subsequence(最长公共子序列LCS)
POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...
- Poj 1458 Common Subsequence(LCS)
一.Description A subsequence of a given sequence is the given sequence with some elements (possible n ...
- poj 1458 Common Subsequence【LCS】
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: 17 ...
- poj 1458 Common Subsequence(dp)
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 46630 Accepted: 19 ...
- OpenJudge/Poj 1458 Common Subsequence
1.链接地址: http://poj.org/problem?id=1458 http://bailian.openjudge.cn/practice/1458/ 2.题目: Common Subse ...
- POJ 1458 Common Subsequence (动态规划)
题目传送门 POJ 1458 Description A subsequence of a given sequence is the given sequence with some element ...
随机推荐
- 一、关于a标签伪类中的visited不起作用问题
一.代码示范 <html> <head> <title>伪类超链接</title> <!--<link href="./test. ...
- 跳转语句之continue
js里面有两个跳转语句,一个是continue,一个是break.由于这两个跳转语句都是用于循环当中,因此他们也就只能用于while.for.do…while语句中,当然了,break多加一个swit ...
- 学习 Spring (八) 注解之 Bean 的定义及作用域
Spring入门篇 学习笔记 Classpath 扫描与组件管理 从 Spring 3.0 开始,Spring JavaConfig 项目提供了很多特性,包括使用 java 而不是 XML 定义 be ...
- 微服务 Micro services
微服务 (Microservices) 是一种软件架构风格,它是以专注于单一责任与功能的小型功能区块 (Small Building Blocks) 为基础,利用模组化的方式组合出复杂的大型应用程序, ...
- hdu 4578 Transformation 线段树多种操作裸题
自己写了一个带结构体的WA了7.8次 但是测了几组小数据都对..感觉问题应该出在模运算那里.写完这波题解去对拍一下. 以后线段树绝不写struct!一般的struct都带上l,r 但是一条线段的长度确 ...
- codeforces498C
Array and Operations CodeForces - 498C You have written on a piece of paper an array of n positive i ...
- codeforces362B
Petya and Staircases CodeForces - 362B 题意: 一个小男孩要上楼梯,他一次可以走1个台阶或2个台阶或3个台阶,但是有一些台阶是脏的,他不想走在脏台阶上.一共有n个 ...
- 第四十天 并发编程之io模型
一.今日内容 1.网络IO的两个阶段 waitdata copydata 2阻塞IO模型 之前写的都是阻塞 无论多线程 多进程 还是 进程池 线程池 3.非阻塞IO模型 在非阻塞IO中 需要不断循环询 ...
- 多校#5-1005-Instring-HDU5785-manacher+维护
寻找三元组(i,j,k),使得(i,j)(j,k)都是回文字串,其中i<=j<k. 可以发现,对于每一位i,只要预处理出来有多少个以i为右端的回文串和以i为左端的回文串.把那些串的另一端的 ...
- 洛谷P4782 2-SAT问题
2-SAT问题 这是一道2-SAT的模板题.对于2-SAT问题的每一个条件,我们需要把他们转化成可接受的条件.即"若变量A的赋值为x,则变量B的赋值为y",其中x,y均等于0或1. ...