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. django 配置上传图片和文件

    在django中经常遇到要上传文件的需求,这里记录下如何配置用户上传的文件保存 首先在setting中添加 TEMPLATES = [ { 'BACKEND': 'django.template.ba ...

  2. java中XML操作:xml与string互转、读取XML文档节点及对XML节点增删改查

    一.XML和String互转: 使用dom4j程式变得很简单 //字符串转XML String xmlStr = \"......\"; Document document = D ...

  3. How to support both ipv4 and ipv6 address for JAVA code.

    IPv6 have colon character, for example FF:00::EEIf concatenate URL String, IPv6 URL will like: http: ...

  4. ES怎么进行字段添加索引,并保留原有数据

    1.先将原索引进行备份 curl -XPOST '192.168.46.163:9200/_reindex?pretty' -H 'Content-Type: application/json' -d ...

  5. 一分钟sed入门

    [转载于58同城沈剑] 1.简介 sed是一种行编辑器,它一次处理一行内容. 2.sed调用方式 sed [options] 'command' file(s) sed [options] -f sc ...

  6. selenium执行报错:Process refused to die after 10 seconds, and couldn't taskkill it

    十二月 02, 2015 5:16:56 下午 org.openqa.selenium.os.ProcessUtils killWinProcess 警告: Process refused to di ...

  7. 时间操作(JavaScript版)—最简单比較两个时间格式数据的大小

    呵呵呵,在软件研发过程中假设遇到要比較两个时间的大小.你会怎么做.嗯嗯嗯,非常直观的做法就是把"-"去掉,再比較大小,真的有必要吗?看以下最简单的时间比較方式: <!DOCT ...

  8. A read-only user or a user in a read-only database is not permitted to disable

    A read-only user or a user in a read-only database is not permitted to disable 出现如题的问题通常是由于db.lck的所属 ...

  9. Tomcat Connector的三种不同的运行模式

    Tomcat Connector的三种不同的运行模式性能相差很大,有人测试过的结果如下: 这三种模式的不同之处如下: BIO: 一个线程处理一个请求.缺点:并发量高时,线程数较多,浪费资源. Tomc ...

  10. 微信小程序 - .gitignore失效问题

    -------------------------------------------- Last Update Date:2018-8-8 ----------------------------- ...