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正则表达式的使用

    前言:正则表达式(regular expression)反反复复学了多次,学了又忘,忘了又学,这次打算把基本的东西都整理出来,加强记忆,也方便下次查询. 学习正则表达式之前首先需要掌握记忆这些基本概念 ...

  2. es6函数的rest参数和拓展运算符(...)的解析

    es6的新特性对函数的功能新增加了rest参数和...的拓展运算符.这是两个什么东西呢? 先来看一个问题:如何获取一个函数除了定义的参数之外的其他参数?传统的做法是借助函数的arguments关键字来 ...

  3. Codeforces 858A. k-rounding 数论

    题目: 题意:输入n和k,找到一个最小的数,满足末尾有至少k个0和是n的倍数. 最小的情况 ans = n,最大的情况 ans = n*pow(10,k). 令 k = pow(10,k); 我们发现 ...

  4. http://codeforces.com/contest/535/problem/C

    C. Tavas and Karafs time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. Django的form表单之文件上传

    在生成input标签的时候可以指定input标签的类型为file类型 <!DOCTYPE html> <html lang="en"> <head&g ...

  6. Centos7搭建swarm集群

    1. 准备 两台虚拟机,IP分别为: 192.168.1.104 192.168.1.105 保证能互相 ping 通 2. 修改虚拟机的 host,分别任 c1.c2 在 192.168.1.105 ...

  7. Django进阶篇【2】

    在学习之前,我们补充一个知识点(static用法) 创建APP 配置: setting.py STATICFILES_DIRS = ( os.path.join(BASE_DIR,'static') ...

  8. 集群提交spark任务命令

    >>spark-submit --class WordCount  DataMining.jar /dept_ana/part-00000 /dept_ana/output/wordCou ...

  9. jQuery中下拉select、复选checkbox、单选radio的操作代码

    //select $("#Icon") //对象 $("#Icon").val() //取值 $("#Icon").val("fa ...

  10. webpack2使用ch10-处理图片(png jpg svg 等) 限制图片 压缩图片

    1 目录展示 安装依赖 "file-loader": "^0.11.1", "image-webpack-loader": "^3 ...