OpenJudge/Poj 1458 Common Subsequence
1.链接地址:
http://poj.org/problem?id=1458
http://bailian.openjudge.cn/practice/1458/
2.题目:
Common Subsequence
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35411 Accepted: 14080 Description
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequence of X if there exists a strictly increasing sequence < i1, i2, ..., 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.Input
The program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.Output
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.Sample Input
abcfbc abfcab
programming contest
abcd mnpSample Output
4
2
0Source
3.思路:
4.代码:
#include "stdio.h" //#include "stdlib.h" #include "string.h" #define N 1000 char a[N],b[N]; int c[N+][N+]; int dp(int lena,int lenb) { int i,j; for(i=;i<=lena;i++) {c[i][]=;} for(j=;j<=lenb;j++) {c[][j]=;} for(i=;i<=lena;i++) { for(j=;j<=lenb;j++) { if(a[i-] == b[j-]) {c[i][j]=c[i-][j-]+;} //if(i==1 && j==1){printf("%c %c\n",a[0],b[0]);} else { c[i][j]=(c[i][j-]>c[i-][j])?c[i][j-]:c[i-][j]; } //printf("%c %c %d\n",a[i-1],b[j-1],c[i][j]); } } return c[lena][lenb]; } int main() { int i,j; while(scanf("%s%s",a,b) != EOF) { printf("%d\n",dp(strlen(a),strlen(b))); //for(i=0;i<strlen(a);i++){for(j=0;j<strlen(b);j++){printf("%d ",c[i][j]);}printf("\n");} } //system("pause"); return ; }
OpenJudge/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(最长公共子序列LCS)
POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...
- POJ 1458 Common Subsequence (动态规划)
题目传送门 POJ 1458 Description A subsequence of a given sequence is the given sequence with some element ...
- 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
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 46387 Accepted: 19 ...
- poj 1458 Common Subsequence【LCS】
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: 17 ...
- (线性dp,LCS) POJ 1458 Common Subsequence
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65333 Accepted: 27 ...
- POJ - 1458 Common Subsequence DP最长公共子序列(LCS)
Common Subsequence A subsequence of a given sequence is the given sequence with some elements (possi ...
随机推荐
- 使用CATransition实现页面的“从左向右” “从右向左”的动画
-(void)initView{ UISwipeGestureRecognizer *left_gesture=[[UISwipeGestureRecognizer alloc]initWithTar ...
- 感动前行——给医学媳妇写的演讲稿(非IT类)
感动前行 我是一个平庸的人,走在人群中大家可能不能辨别出我.我是一个平庸的人,每天上班.工作.吃饭.睡觉.我是一个平庸的人,来了医院多半年也仅仅和检验科的同事相对照较熟悉,其它科室人员非常少有交流. ...
- 【10】令operator=返回一个reference to *this
1.令operator= 返回一个reference to *this,为什么? 这只是一个协议,并无强制性.但是,为了与基本类型的行为保持一致性,强烈建议这么做.设计class 有一个宝典:一旦有疑 ...
- 【转】C++ function、bind以及lamda表达式
本文是C++0x系列的第四篇,主要是内容是C++0x中新增的lambda表达式, function对象和bind机制.之所以把这三块放在一起讲,是因为这三块之间有着非常密切的关系,通过对比学习,加深对 ...
- esui控件validatebox 通过正则判断输入 json传值
<td> @Html.TextBoxFor(m => m.ActualInvoiceFee, new { @id = "txtActualInvoiceFee", ...
- iOS 音频拼接
工作中或许会遇到这样的需求,将两段不同的音频合成一个音频(暂且称之为音频拼接),实现起来相对来说不是很难,再介绍如何拼接之前,先了解下AVFoundation下的几个基本知识点. 基本知识 AVAss ...
- C 循环链表
循环链表的定义:将单链表中最后一个数据元素的next指针指向第一个元素 在循环链表中可以定义一个“当前”指针,这个指针通常称为游标,可以通过这个游标来遍历链表中的所有元素. 1) 普通插入元素(和单链 ...
- 进程关系之tcgetpgrp、tcsetpgrp和tcgetsid函数
需要有一种方法来通知内核哪一个进程组是前台进程组,这样,终端设备驱动程序就能了解将终端输入和终端产生的信号送到何处. #include <unistd.h> pid_t tcgetpgrp ...
- ADO.Net的小知识(连接数据库)二
上次提到数据库连接有两种形式断开式连接和打开式连接,断开式连接我已经讲解了,下面我来给大家讲解一下打开式连接 (1)引入命名空间:using System.Data.SqlClient; 该语句用于导 ...
- vb.net中常用键值
可在代码中的任何地方用下列常数代替实际值: 常数 值 描述 vbKeyLButton 0x1 鼠标左键 vbKeyRButton 0x2 鼠标右键 vbKeyCancel 0x3 CANCEL 键 v ...