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. 
 

Sample Input

abcfbc abfcab
programming contest
abcd mnp
 

Sample Output

4
2
0
 
 
分析:

  设c[i][j]为字符串a的第i个字符与字符串b的第j个字符为止的最长公共子序列长度,那么有两种情况:

  1. 当a[i] == b[j]时,c[i][j]应该是前一个状态的最长公共子序列长度 + 1,因为a[i]与b[j]匹配,a[i]与b[j]必然不能已经匹配过,否则就是同一个字母匹配了多次,这必然是非法的,因此上一个状态应是c[i - 1][j - 1],即c[i][j] = c[i - 1][j - 1] + 1;
  2. 当a[i] != b[j]时,上一个状态可能是c[i - 1][j]或c[i][j - 1],而既然要找最长公共子序列,自然是找最大的一个,即c[i][j] = max(c[i - 1][j], c[i][j - 1])。
AC代码:
 #include<iostream>
#include<string>
using namespace std;
int dp[][];
int main()
{
string a,b;
while(cin>>a>>b)
{
int alen=a.length();
int blen=b.length();
memset(dp,,sizeof(dp));
for(int i=;i<=alen;i++)
{
for(int j=;j<=blen;j++)
{
if(a[i-]==b[j-])
dp[i][j]=dp[i-][j-]+;
else
dp[i][j]=max(dp[i-][j],dp[i][j-]);
}
}
for(int i=;i<=alen;i++)
{
for(int j=;j<=blen;j++)
cout<<dp[i][j]<<" ";
cout<<endl;
}
cout<<dp[alen][blen]<<endl;
}
return ;
}

解题报告 HDU1159 Common Subsequence的更多相关文章

  1. HDU-1159 Common Subsequence 最长上升子序列

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  2. PAT 解题报告 1007. Maximum Subsequence Sum (25)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

  3. Lintcode:Longest Common Subsequence 解题报告

    Longest Common Subsequence 原题链接:http://lintcode.com/zh-cn/problem/longest-common-subsequence/ Given ...

  4. 题解报告:hdu 1159 Common Subsequence(最长公共子序列LCS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Problem Description 给定序列的子序列是给定的序列,其中有一些元素(可能没有) ...

  5. 【九度OJ】题目1439:Least Common Multiple 解题报告

    [九度OJ]题目1439:Least Common Multiple 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1439 ...

  6. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  7. 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)

    [LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...

  8. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  9. 【LeetCode】392. Is Subsequence 解题报告(Python)

    [LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...

随机推荐

  1. ostringstream的使用方法

    ostringstream的使用方法 [本文来自]http://www.builder.com.cn/2003/0304/83250.shtml http://www.cppblog.com/alan ...

  2. linux之线程

    http://blog.csdn.net/lanyan822/article/details/7586845 POSIX线程数据类型: pthread_t 线程标识符: pthread_mutex_t ...

  3. jquery 插件 validate 学习

    jquery是十分方便的对于现在来说. 首先应该明白一个问题: <p> <label  for="password">Password</label& ...

  4. Ajax+asp.net实现用户登陆 转自http://www.shangxueba.com/jingyan/2933319.html

    这篇文章主要介绍了Ajax+asp.net实现用户登陆,主要是为了练习ajax的使用方法,有需要的小伙伴参考下. 以用户登录为例练习ajax的使用方法 login.html <!DOCTYPE ...

  5. 小猪猪逆袭成博士之C++基础篇(一)数据精度、强制类型转换、变量命名规则

    小猪猪逆袭成博士之C++基础篇(一) 关键词: 数据精度.强制类型转换.变量命名规则 综述: 1.大多数编程语言通过两种方式来进一步补充其特征:一是赋予程序员自定义数据类型的权利(C++中的类):二是 ...

  6. Find the k-th Smallest Element in the Union of Two Sorted Arrays

    (http://leetcode.com/2011/01/find-k-th-smallest-element-in-union-of.html) Given two sorted arrays A, ...

  7. Html中截切文章内容,造成标签不全的问题

    把标签全部进行替换 ) { string strText = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]+ ...

  8. Media Queries for Standard Devices

    /* Smartphones (portrait and landscape) ----------- */ @media only screen and (min-device-width : 32 ...

  9. mybatis字段名与类属性名不相同的冲突

    新建一个表 CREATE TABLE orders( order_id INT PRIMARY KEY AUTO_INCREMENT, order_no ), order_price FLOAT ); ...

  10. 大数据情报分析公司Palantir

    最近在学习图数据计算方面技术,在寻找现实应用时发现美国Palantir公司已将所谓的多源异构数据融合分析技术运用的炉火纯青.Palantir创立于2004年,最早是因PayPal公司为保障支付安全而逐 ...