C - Common Subsequence

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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. 
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. 

Input

abcfbc abfcab
programming contest
abcd mnp

Output

4
2
0

Sample Input

abcfbc abfcab
programming contest
abcd mnp

Sample Output

4
2
0 //求两个字符串的最长公共子序列长度 //虽然是 dp 序列水题,但是我第一次做不会,没想到转移方程 代码里写的很清楚了,31ms dp[i][j]表示0到i-1跟0到j-1的最长公共子序列长度
 #include <stdio.h>
#include <string.h> char a[];
char b[];
int dp[][]; int max(int x,int y)
{return x>y?x:y;} int main()
{
int i,j;
while(scanf("%s%s",a,b)!=EOF)
{
int la=strlen(a),lb=strlen(b);
for (i=;i<=lb;i++)
dp[][i]=;
for (i=;i<=la;i++)
dp[i][]=;
for (i=;i<=la;i++)
{
for (j=;j<=lb;j++)
{
if (a[i-]==b[j-])
dp[i][j]=dp[i-][j-]+;
else
dp[i][j]=max(dp[i-][j],dp[i][j-]);
}
}
printf("%d\n",dp[la][lb]);
}
return ;
}
 

C - Common Subsequence的更多相关文章

  1. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  2. LintCode Longest Common Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...

  3. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  4. LCS(Longest Common Subsequence 最长公共子序列)

    最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...

  5. Longest Common Subsequence

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  6. LCS POJ 1458 Common Subsequence

    题目传送门 题意:输出两字符串的最长公共子序列长度 分析:LCS(Longest Common Subsequence)裸题.状态转移方程:dp[i+1][j+1] = dp[i][j] + 1; ( ...

  7. Common Subsequence LCS

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/F 题目: Description A subsequ ...

  8. poj 1458 Common Subsequence

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

  9. Longest Increasing Common Subsequence (LICS)

    最长上升公共子序列(Longest Increasing Common Subsequence,LICS)也是经典DP问题,是LCS与LIS的混合. Problem 求数列 a[1..n], b[1. ...

  10. Common Subsequence(dp)

    Common Subsequence Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 951  Solved: 374 Description A subs ...

随机推荐

  1. checkStyle总结

    参考网站:https://code.google.com/p/testcq/wiki/CheckStyleRules 1.Variable access definition in wrong ord ...

  2. Yum编译安装Error Downloading Packages报错

    1:执行yum clean all 清除缓存目录下的软件包及旧的headers: 2:接着执行 yum list重新列出所有已经安装和可以安装的软件包: 3:重新执行上述命令,发现yum编译成功: 注 ...

  3. windows 10 超级优化提速 附系统服务列表纯净

    如图,本机安装了vs2017 office2016 迅雷.谷歌浏览器,不建议安装其它任何软件.vs2017为开发软件,用于编程,一般用户用不到. 如果想安装其它的软件,建议优先使用绿色版本的. 下载服 ...

  4. eclipse maven项目导入Intellij问题处理

    1.maven打包编译时后台一直输出警告信息 [WARNING] File encoding has not been set, using platform encoding GBK, i.e. b ...

  5. 又见The request sent by the client was syntactically incorrect ()

    前几天遇到过这个问题(Ref:http://www.cnblogs.com/xiandedanteng/p/4168609.html),问题在页面的组件name和和注解的@param名匹配不对,这个好 ...

  6. java学习笔记——可用链表

    NO 链表方法名称 描述 1 public void add(数据类型 对象) 向链表中增加数据 2 public int size() 查看链表中数据个数 3 public boolean isEm ...

  7. Win10系统如何关闭"启用病毒防护""启用Windows防火墙"提示?

    Win10系统如何关闭"启用病毒防护""启用Windows防火墙"提示?   目前已经有不少有用户升级到了win10正式版系统,不过有一些原win7/win8. ...

  8. eclipse上的git命令使用浅析,搭建Maven项目

    eclipse上的git命令使用浅析 2016-03-31 14:44   关于eclipse上git的安装和建立代码仓库的文章比较多,但作为一个初识git的人更希望了解每个命令的作用. 当项目连接到 ...

  9. 网上流传的长盛不衰的Steve Jobs(乔布斯) 14分钟“Stay Hungry, Stay Foolish”演讲视频

    http://timyang.net/misc/speech/附:网上流传的长盛不衰的Steve Jobs 14分钟“Stay Hungry, Stay Foolish”演讲视频 (原视频地址:htt ...

  10. ios 使用gcd 显示倒计时

    __block ;//倒计时时间 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ...