Problem B

时间:2022-11-14 09:50:20

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&colon; 大小关系

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

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

    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 &lpar;卡特兰数&rpar;

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

  7. BZOJ2301&colon; &lbrack;HAOI2011&rsqb;Problem b&lbrack;莫比乌斯反演 容斥原理&rsqb;【学习笔记】

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

  8. &lbrack;LeetCode&rsqb; 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. &lbrack;LeetCode&rsqb; 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报错&OpenCurlyDoubleQuote;Problem &lpar;2&rpar; in the Chunked-Encoded data”解决方案

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

随机推荐

  1. &lpar;十二&rpar;select&lpar;&rpar;函数以及FD&lowbar;ZERO、FD&lowbar;SET、FD&lowbar;CLR、FD&lowbar;ISSET

    select函数用于在非阻塞中,当一个套接字或一组套接字有信号时通知你,系统提供select函数来实现多路复用输入/输出模型,原型:int select(int maxfd,fd_set *rdset ...

  2. eclipse中将项目发布到tomcat的root目录

    在eclipse中,将项目直接部署在tomcat的root目录中,这样便可以直接ip:port访问项目: 项目右键->属性->web project settings

  3. java8中map的meger方法的使用

    java8中map有一个merge方法使用示例: /** * 打印出包含号码集的label的集合 * * @param args */ public static void main(String[] ...

  4. python 正则re模块

    re.match re.match 尝试从字符串的开始匹配一个模式,如:下面的例子匹配第一个单词. import re text = "JGood is a handsome boy, he ...

  5. 在unix系统下的 &period;o文件 &period;a文件 &period;so文件说明和相互关系

    .o文件 .o文件就是对象文件,包含编译好的可执行代码,当程序执行时,被链接库链接调用[相当于windows里的obj文件] .a文件unix中的静态链接库,包含多个需要包含的.o文件,主要特点是在 ...

  6. 面试题之【打印1到最大的N位数】

    题目描述:给定一个数字N,打印从1到最大的N位数. 看起来像是很简单的问题(虽然实际也不是很难...)我们很容易写出这样的代码: #include<iostream> #include&l ...

  7. JS全选

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  8. 通过MFC设计一个简单的计价程序

    1.实验目的 掌握使用MFC应用程序向导创建应用程序的方法. 掌握新建对话框资源的方法. 掌握生成对话框的方法. 2.实验内容 用应用程序创建一个默认的对话框应用程序,在对话框中能进入下一个对话框,在 ...

  9. 数据层的多租户浅谈(SAAS多租户数据库设计)

    在上一篇“浅析多租户在 Java 平台和某些 PaaS 上的实现”中我们谈到了应用层面的多租户架构,涉及到 PaaS.JVM.OS 等,与之相应的是数据层也有多租户的支持. 数据层的多租户综述 多租户 ...

  10. featuremap尺寸的计算

    对于卷积层,向下取整 对于池化层:想上取整 output=((input+2*pad-dilation*(kernel-1)+1)/stride)+1 input:输入尺寸 output:输出尺寸 p ...