问题 L: Common Subsequence

时间限制: 1 Sec  内存限制: 32 MB
提交: 70  解决: 40
[提交][状态][讨论版]

题目描述

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

..在大神的指点下,似乎明白了点啥。智商需要充值。假设这俩字符串为a与bC[i][j]来表示当前移动到a的第i个位置,b的第j个位置时所产生的最大公共子字符个数。

C[i][j]要么保持上两个状态中的一个,要么出现了公共的字符进行+1操作。

list[i+1][j+1]=(a[i]==b[j] ? list[i][j]+1 : max( list[i+1][j] , list[i][j+1]) );

代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<cstring>
#include<cmath>
using namespace std;
int list[1010][1010];
int main(void)
{
int t,i,j,ans;
string a,b;
while (cin>>a>>b)
{
memset(list,0,sizeof(list));
int la=(int)a.size();
int lb=(int)b.size();
for (i=0; i<la; i++)
{
for (j=0; j<lb; j++)
{
list[i+1][j+1]=(a[i]==b[j]?list[i][j]+1:max(list[i+1][j],list[i][j+1]));//状态转移方程
}
}
printf("%d\n",list[la][lb]);
}
return 0;
}

ACM程序设计选修课——1018: Common Subsequence(DP)的更多相关文章

  1. Common Subsequence(dp)

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

  2. UVA 10405 Longest Common Subsequence (dp + LCS)

    Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...

  3. POJ1458 Common Subsequence —— DP 最长公共子序列(LCS)

    题目链接:http://poj.org/problem?id=1458 Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  4. POJ - 1458 Common Subsequence DP最长公共子序列(LCS)

    Common Subsequence A subsequence of a given sequence is the given sequence with some elements (possi ...

  5. Longest Common Subsequence (DP)

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

  6. HDU 1159 Common Subsequence --- DP入门之最长公共子序列

    题目链接 基础的最长公共子序列 #include <bits/stdc++.h> using namespace std; ; char c[maxn],d[maxn]; int dp[m ...

  7. CF 346B. Lucky Common Subsequence(DP+KMP)

    这题确实很棒..又是无想法..其实是AC自动机+DP的感觉,但是只有一个串,用kmp就行了. dp[i][j][k],k代表前缀为virus[k]的状态,len表示其他所有状态串,处理出Ac[len] ...

  8. ACM程序设计选修课——1043: Radical loves integer sequences(YY)

    1043: Radical loves integer sequences Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 36  Solved: 4 ...

  9. ACM程序设计选修课——1036: Hungar的菜鸟赛季(YY)

    1036: Hungar的菜鸟赛季 Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 20  Solved: 14 [Submit][Status][Web ...

随机推荐

  1. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] A A Problem about Polyline(数学)

    题目中给出的函数具有周期性,总可以移动到第一个周期内,当然,a<b则无解. 假设移动后在上升的那段,则有a-2*x*n=b,注意限制条件x≥b,n是整数,则n≤(a-b)/(2*b).满足条件的 ...

  2. Aho-Corasick自动机

    在模式匹配问题中,如果模板有很多个,KMP算法就不太适合了.因为每次查找一个模板.都要遍历整个文本串.可不可以只遍历一次文本串呢?可以,方法是把所有模板组成一个大的状态转移图(称为$Aho-Coras ...

  3. 影响一个UIView是否能正常显示的几个因素

    在使用代码实现UIView及其子类的对象的时候,经常会遇到创建的某个view没有显示在屏幕上.以下总结了本人遇到过的几种情况.可能还有些其它的原因也会导致view不能正常显示,限于个人经历有限,无法全 ...

  4. falling object思路总结

    1.用检测的方法把falling object标记为一个类别,然后检测出类别.这种方式不可行的原因:因为falling object可能是任何东西,所以可能是一个路锥,也可能是一个玻璃瓶,还可能是掉下 ...

  5. javaweb基础(10)_HttpServletRequest原理介绍

    一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...

  6. C#动态数组ArrayList

    在C#中,如果需要数组的长度和元素的个数随着程序的运行不断改变,就可以使用ArrayList类,该类是一个可以动态增减成员的数组. 一.ArrayList类的常用属性和方法 1. ArrayList类 ...

  7. Unicode与ASCiI之间有什么区别?java当中的转义字符 Character类的使用 String类的使用

    ASCII码 称为 美国标准信息交换码 (American standard code of Information Interchange) 其中一共有多少个码?2的7次幂 128个 Unicode ...

  8. iOS项目工程及目录结构

    做过一些iOS的项目,不同项目的沉淀没有积累到一起,目录的管理都在后期随着人员的增加越来越混乱,因此在这里做一些梳理,希望达到两个目的. 一套相对通用的目录结构,作为后续项目的模版. 积累相应的基础库 ...

  9. C++实现Singleton模式(effective c++ 04)

    阅读effective c++ 04 (31页) 提到的singleton设计模式.了解一下. 定义: 保证一个类仅有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享. 应用场景: 比 ...

  10. Linux基础学习-NFS网络文件系统实时文件共享

    NFS网络文件系统 如果大家觉得Samba服务程序的配置太麻烦了,那么你共享文件的主机都是Linux系统,那么推荐大家在客户端部署nfs服务来共享文件.nfs(网络文件系统)服务可以将远程Linux系 ...