Problem 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

题意:给你两个字符数列,求相似度(最长公共子序列)

解题思路:动态规划,看了一下午课件没找到最长公共子序列的例题,在一个博客上看到了动态规划的讲解,刚开始还不大明白状态转移方程是怎么回事,,照着写了一遍,看看图对着代码走了一遍就明白了,在二维数组中查找相同的元素,DP[i][j]记录当前状态的最长公共子序列的长度,另外如果用一个flag[i][j]记录状态转移的过程,就可以输出最长公共子序列;

感悟:动态规划太抽象了,但是想出来了,就很好谢了;

代码:

#include

#include

#include

#define maxn 1005

using namespace std;

char s1[maxn],s2[maxn];

int dp[maxn][maxn];//记录当前状态的最长子序列长度

int main()

{

//freopen("in.txt","r",stdin);

while(~scanf("%s",&s1))

{

scanf("%s",&s2);

for(int i=1;i<=strlen(s1);i++)

{

for(int j=1;j<=strlen(s2);j++)

{

if(s1[i-1]==s2[j-1])

dp[i][j]=dp[i-1][j-1]+1;

else if(dp[i][j-1]>dp[i-1][j])

dp[i][j]=dp[i][j-1];

else

dp[i][j]=dp[i-1][j];

}

}

printf("%d\n",dp[strlen(s1)][strlen(s2)]);

}

}

Problem B的更多相关文章

  1. 1199 Problem B: 大小关系

    求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...

  2. No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

    Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...

  3. C - NP-Hard Problem(二分图判定-染色法)

    C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144 ...

  4. Time Consume Problem

    I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...

  5. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

  6. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  7. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  8. [LeetCode] Water and Jug Problem 水罐问题

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  9. [LeetCode] The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  10. PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案

    $s = curl_init(); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $queryStr ...

随机推荐

  1. [js高手之路] es6系列教程 - 迭代器与生成器详解

    什么是迭代器? 迭代器是一种特殊对象,这种对象具有以下特点: 1,所有对象都有一个next方法 2,每次调用next方法,都会返回一个对象,该对象包含两个属性,一个是value, 表示下一个将要返回的 ...

  2. NopCommerce添加事务机制

    NopCommerce现在最新版是3.9,不过依然没有事务机制.作为一个商城,我觉得事务也还是很有必要的.以下事务代码以3.9版本作为参考: 首先,IDbContext接口继承IDisposable接 ...

  3. Azure SQL Database (25) Azure SQL Database创建只读用户

    <Windows Azure Platform 系列文章目录> 本文将介绍如何在Azure SQL Database创建只读用户. 请先按照笔者之前的文章:Azure SQL Databa ...

  4. DOM中元素对象的属性方法

    在 HTML DOM (文档对象模型)中,每个部分都是节点. 节点是DOM结构中最基本的组成单元,每一个HTML标签都是DOM结构的节点. 文档是一个    文档节点 . 所有的HTML元素都是    ...

  5. iOS 获取设备信息,mac地址,IP地址,设备名称

    #import "DeviceInfoUtil.h" #import "GlobleData.h" #import "sys/utsname.h&qu ...

  6. 用ESP8266+android,制作自己的WIFI小车

    整体思路ESP8266作为TCP服务器,,手机作为TCP客户端,自己使用Lua直接做到了芯片里面,省了单片机,,节约成本,其实本来就是个单片机(感觉Lua开发8266真的很好,甩AT指令好几条街,,而 ...

  7. [bzoj1066] [SCOI2007] 蜥蜴 - 网络流

    在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到平面距离不超过d的任何一个 ...

  8. PE文件格式详解,第一讲,DOS头文件格式

    PE文件格式详解,第一讲,DOS头文件格式 今天讲解PE文件格式的DOS头文件格式 首先我们要理解,什么是文件格式,我们常说的EXE可执行程序,就是一个文件格式,那么我们要了解它里面到底存了什么内容 ...

  9. php过滤textarea 中的换行符问题

    之前我写的替换代码是这样的 $content = str_replace('\r\n', '', $_POST['content']); 为了确保window和Linux的换行符都能去掉,改成这样的: ...

  10. Block使用的简单总结

    一.Block简单的使用 1.block当作参数来传递 如下定义一个没有返回值无参数的block,并把它作为参数,让系统调用,注意:这里是系统在调用,不是我们调用 那么为什么需要把block当作参数去 ...