2402: Common Subsequence

时间限制: 1 Sec  内存限制: 32 MB

提交: 63  解决: 33

题目描述

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.


输入

abcfbc abfcab

programming contest 

abcd mnp

输出

4

2

0

样例输入

abcfbc abfcab
programming contest
abcd mnp

样例输出

4
2
0

迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char s1[1000],s2[1000];
int dp[1000][1000];
int len1,len2;
void LCS()
{
int i,j;
memset(dp,0,sizeof(dp));
for(i = 1; i<=len1; i++)
{
for(j = 1; j<=len2; j++)
{
if(s1[i-1] == s2[j-1])
dp[i][j] = dp[i-1][j-1]+1;
else
dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
}
}
}
int main()
{
while(~scanf("%s%s",s1,s2))
{
len1 = strlen(s1);
len2 = strlen(s2);
LCS();
printf("%d\n",dp[len1][len2]);
}
return 0;
}

YTU 2402: 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. ...

随机推荐

  1. 大数据学习——下载集群根目录下的文件到E盘

    代码如下: package cn.itcast.hdfs; import java.io.IOException; import org.apache.hadoop.conf.Configuratio ...

  2. [CTSC2007]数据备份Backup 题解

    题意: 一维直线上有n个点,任取2k个互不相同的点组成k条链,求链的最小总长 思路: 1.最优时链不相交,相邻两两相减,将题目转化为:在n-1个数中取互不相邻的k个数使总和最小. 2.贪心取最小的“数 ...

  3. linux mail 发邮件

    system('echo "'.$xmlHeader.$xmlBody.$xmlFooter.'" | mail -s "百度新闻源生成成功,地址=>http:// ...

  4. mysql 时间类型datetime与timestamp区别比较

    mysql 时间类型datetime与timestamp区别比较 相同点: 显示宽度和格式相同,显示宽度固定在19字符,格式为YYYY-MM-DD HH:MM:SS. 不同点: (1)时间范围不同: ...

  5. Thinkphp5学习 Windows下的安装

    方法一.通过官方网站直接下载: (1)下载地址:http://www.thinkphp.cn/down.html: (2)下载后,解压到web目录下: (3)访问:http://localhost/目 ...

  6. 寒武纪camp Day4

    补题进度:7/11 A(博弈论) 略 B 待填坑 C(贪心) 题意: 一个序列是good的当且仅当相邻两个数字不相同.给出一个长度为n的数列,每个数字是ai.定义一种操作就是把a中某个元素拿到首位去, ...

  7. npm Error: Cannot find module 'proto-list'

    Error: Cannot find module 'proto-list' at Function.Module._resolveFilename (module.js:338:15) at Fun ...

  8. Java重写equals方法和hashCode方法

    package com.ddy; public class User {     private Integer id;     private String name;     private St ...

  9. SHELL脚本实现分区

    写一个脚本(前提:请为虚拟机新增一块硬盘,架设它为/dev/sdb),为指定的硬盘创建分区 1,列出当前系统上所有的磁盘,让用户选择,如果选择quit则退出脚本:如果用户选择错误,就让用户重新选择 2 ...

  10. VS2015 android 设计器不能可视化问题解决。

    近期安装了VS2015,体验了一下android 的开发,按模板创建执行了个,试下效果非常不错.也能够可视化设计.但昨天再次打开或创建一个android程序后,设计界面直接不能显示,显示错误:(可能是 ...