Problem B
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的更多相关文章
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- 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 ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
- Time Consume Problem
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- [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 ...
- [LeetCode] The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案
$s = curl_init(); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $queryStr ...
随机推荐
- 跨Storyboard调用
在开发中我们会有这种需求从一个故事板跳到另一个故事板 modal UIStoryboard *secondStoryboard = [UIStoryboard storyboardWithName:@ ...
- mybatis快速入门(五)
今天写写user表和orders表的mybatis的高级映射,一对一映射和一对多映射 1.创建一个orders.java文件 1.1一对一映射,一条订单对应一个用户 package cn.my.myb ...
- [Node.js] 2、利用node-git-server快速搭建git服务器
本文用到了node-git-server 1.检测本地git版本 该包的使用需要机器上本来就安装git,且git的版本大于等于2.7: ╭─root@lt /home/workspace ╰─# gi ...
- python 库之lxml安装 坑一个
error: command 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python ...
- Nginx学习——Nginx启动、停止、重启和信号控制以及平滑升级
1.Nginx 启动与停止 (1)启动方式 启动格式:Nginx可执行文件地址 -c Nginx配置文件地址 /etc/local/nginx/sbin/nginx -c /root/dufy/ngi ...
- java学习——java中的反射学习笔记
Java--reflect 一.Class类的使用 什么是Class类? 1:在面向对象的世界中,万事万物皆对象. java语言中,静态的成员,普通数据类型类是不是对象呢? 是,对象!是类的对象! 类 ...
- 小米2017秋招真题——电话号码分身问题(Java版)
原题描述如下: 通过对各个数字对应的英文单词的分析,可以发现一些规律: 字母Z为0独占,字母W为2独占,字母U为4独占,字母X为6独占,字母G为8独占: 在过滤一遍0.2.4.6.8后,字母O为1独占 ...
- 程序员网站开发时应该注意的SEO问题
一.链接的统一性 搜索引擎排名最主要的因素就是网站内容和链接,假如网站内部链接不一致,在很大程度上直接影响着网站在搜索引擎中的排名.例如彩票专营店导航栏中的“首页”链接,程序员在开发时可能会有以下几种 ...
- 一个scrapy框架的爬虫(爬取京东图书)
我们的这个爬虫设计来爬取京东图书(jd.com). scrapy框架相信大家比较了解了.里面有很多复杂的机制,超出本文的范围. 1.爬虫spider tips: 1.xpath的语法比较坑,但是你可以 ...
- VB改变文件及文件夹属性的函数
'以下为FSO对象的使用示例(VB6.0) Private myFSO As New FileSystemObject Private myFile As File Private myFolder ...