【水:最长公共子序列】【HDU1159】【Common Subsequence】
Common Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24919 Accepted Submission(s): 11035
..., 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.
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. 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.
abcfbc abfcab
programming contest
abcd mnp
4
2
0
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
using namespace std;
char A[1001],B[1001];
int F[1000][1000];
void solve()
{
memset(F,0,sizeof(F));
int lena=strlen(A);
int lenb=strlen(B);
F[0][0]=0;
F[0][1]=0;
F[1][0]=0;
for(int i=1;i<=lena;i++)
for(int j=1;j<=lenb;j++)
{
if(A[i-1]==B[j-1])
F[i][j]=F[i-1][j-1]+1;
else F[i][j]=max(F[i][j-1],F[i-1][j]);
}
printf("%d\n",F[lena][lenb]);
}
int main()
{
while(scanf("%s %s",A,B)!=EOF)
{
solve();
}
return 0;
}
【水:最长公共子序列】【HDU1159】【Common Subsequence】的更多相关文章
- 最长公共子序列(Longest common subsequence)
问题描述: 给定两个序列 X=<x1, x2, ..., xm>, Y<y1, y2, ..., yn>,求X和Y长度最长的公共子序列.(子序列中的字符不要求连续) 这道题可以 ...
- LCS最长公共子序列HDU1159
最近一直在学习算法,基本上都是在学习动态规划以及字符串.当然,两者交集最经典之一则是LCS问题. 首先LCS的问题基本上就是在字符串a,b之间找到最长的公共子序列,比如 YAOLONGBLOG 和 Y ...
- 最长公共子序列-Hdu1159
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 动态规划---最长公共子序列 hdu1159
hdu1159 题目要求两个字符串最长公共子序列, 状态转换方程 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] ...
- UVA10100:Longest Match(最长公共子序列)&&HDU1458Common Subsequence ( LCS)
题目链接:http://blog.csdn.net/u014361775/article/details/42873875 题目解析: 给定两行字符串序列,输出它们之间最大公共子单词的个数 对于给的两 ...
- 算法实践--最长公共子序列(Longest Common Subsquence)
什么是最长公共子序列 X=ACCG Y=CCAGCA 长度为1的公共子序列: {A} {C} {G} 长度为2的公共子序列:{AC} {CC} {CG} {AG} 长度为3的公共子序列:{ACG} 长 ...
- 算法练习——最长公共子序列的问题(LCS)
问题描述: 对于两个序列X和Y的公共子序列中,长度最长的那个,定义为X和Y的最长公共子序列.X Y 各自字符串有顺序,但是不一定需要相邻. 最长公共子串(Longest Common Subst ...
- [Python]最长公共子序列 VS 最长公共子串[动态规划]
前言 由于原微软开源的基于古老的perl语言的Rouge依赖环境实在难以搭建,遂跟着Rouge论文的描述自行实现. Rouge存在N.L.S.W.SU等几大子评估指标.在复现Rouge-L的函数时,便 ...
- 最长公共子串(Longest common substring)
问题描述: 给定两个序列 X=<x1, x2, ..., xm>, Y<y1, y2, ..., yn>,求X和Y长度最长的公共子串.(子串中的字符要求连续) 这道题和最长公共 ...
- Common Subsequence--poj1458(最长公共子序列)
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43211 Accepted: 17 ...
随机推荐
- Mysql show Status常用参数详解
状态名 作用域 详细解释 Aborted_clients Global 由于客户端没有正确关闭连接导致客户端终止而中断的连接数 Aborted_connects Global 试图连接到MySQL服务 ...
- stagefright框架(七)-Audio和Video的同步
讲完了audio和video的处理流程,接下来要看的是audio和video同步化(synchronization)的问题.OpenCORE的做法是设置一个主clock,而audio和video就分别 ...
- sql 中的时间处理问题
select GETDATE() as '当前日期',DateName(year,GetDate()) as '年',DateName(month,GetDate()) as '月',DateName ...
- js继承 prototype和constructor
function a(){ this.name="aaa"; } function b(){ ...
- HTML 5 全局属性和事件属性
1.HTML 5 全局属性 HTML 属性能够赋予元素含义和语境. 下面的全局属性可用于任何 HTML5 元素. NEW:HTML 5 中新的全局属性. 属性 描述 accesskey 规定访问元素的 ...
- trangleProble switch方法 java
public class trangleProblem { static int res=1; int codePart=1; int n=100; Stack<Param> stack= ...
- VMVare虚拟机的异常处理---内部错误
由于昨天晚上关闭虚机时手快,直接把机器睡眠了.今天早上开机后,虚机显示启动状态,但是无法连接.搜了一下,网上说删掉.lk锁文件就可以了,删掉后,没有反应,强制关闭,再次启动VM,还是打不开,显示内部错 ...
- NSArray 数组操作
/*---------------------------创建数组------------------------------*/ //NSArray *array = [[NSArray alloc ...
- 【笔记】JS中的数组方法
push()方法:可以向数组的末尾添加一个或者多个元素,并且返回新的长度 pop()方法:可以删除数组最后一个元素,并且返回被删除的元素,注意:如果数组是空的,该方法不进行任何操作,返回undef ...
- HDU 5727 - Necklace
题意:( 0 <= n <= 9 ) 现在有n颗阴珠子和n颗阳珠子,将它们阴阳相间圆排列构成一个环, 已知有些阴珠子和阳珠子不能放在相邻的位置,否则这颗阳珠子就会失去功效, ...