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. 《JavaScript闯关记》视频版硬广

    <JavaScript闯关记>视频版硬广 stone 在菜航工作时,兼任内部培训讲师,主要负责 JavaScript 基础培训,2016年整理的<JavaScript闯关记>课 ...

  2. 基于React Native的移动平台研发实践分享

    转载:http://blog.csdn.net/haozhenming/article/details/72772787 本文目录: 一.React Native 已经成为了移动前端技术的趋势 二.基 ...

  3. Spring 5:以函数式方式注册 Bean

    http://www.baeldung.com/spring-5-functional-beans 作者:Loredana Crusoveanu 译者:http://oopsguy.com 1.概述 ...

  4. vue传数据到模态框中

    写一下我的做法 <a data-toggle="modal" data-target="#delete_tpl_modal" class="bt ...

  5. Python s12 Day1 笔记及作业

    作业一: 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定 python3代码 : # -*- coding:utf-8 -*- input_name = input("Please ...

  6. Quartz入门案例与介绍(与spring整合)

    Quartz概述 Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单或 ...

  7. Python实战之字符串的详细简单练习

    ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__' ...

  8. zoj3961(区间问题)

    点击打开zoj1961Let's Chat Time Limit: 1 Second      Memory Limit:65536 KB ACM (ACMers' Chatting Messenge ...

  9. Java高新技术 Myeclipse 介绍

      Java高新技术   Myeclipse 介绍 知识概述:              (1)Myeclipse开发工具介绍 (2)Myeclipse常用开发步骤详解               ...

  10. IDL 矩阵运算

    矩阵相乘,A#B表示A的列乘以B的行,要求A的行数必须跟B的列数一致 IDL> A=[[0,1,2],[3,4,5]] IDL> B=[[0,1],[2,3],[4,5]] IDL> ...