Common Subsequence 最大公共子序列问题
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.
programming contest
abcd mnp
2
0
#include<stdio.h>
#include<string.h>
int c[500][500],lena,lenb;
int max(int a,int b){
if(a>=b)
return a;
else
return b;
}
int main(){
char a[500],b[500];
while(scanf("%s%s",a,b)==2){
lena = strlen(a);
lenb = strlen(b);
for(int i=0;i<lena;i++)
c[i][0]=0;
for(int j=0;j<lenb;j++)
c[0][j]=0;
for(int i=1;i<=lena;i++)
for(int j=1;j<=lenb;j++){
if(a[i-1]==b[j-1])//这里需要注意下,不能忘记字符a[0],b[0]的比较,所以在计算的过程中需要计算到lena
c[i][j]=c[i-1][j-1]+1;
else
c[i][j]=max(c[i-1][j],c[i][j-1]);
}
printf("%d\n",c[lena][lenb]);
}
return 0;
}
Common Subsequence 最大公共子序列问题的更多相关文章
- 算法:Common Subsequence(动态规划 Java 最长子序列)
Description A subsequence of a given sequence is the given sequence with some elements (possible non ...
- spoj Longest Common Substring (多串求最大公共子序列)
题目链接: https://vjudge.net/problem/SPOJ-LCS 题意: 最多10行字符串 求最大公共子序列 数据范围: $1\leq |S| \leq100000$ 分析: 让他们 ...
- Common Subsequence Gym - 102307C 公共序列
2019 ICPC Universidad Nacional de Colombia Programming Contest C D J C. Common Subsequence 题意:给出长度 ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- LCS(Longest Common Subsequence 最长公共子序列)
最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...
- POJ 1458 Common Subsequence(LCS最长公共子序列)
POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...
- LCS最大公共子序列问题
在生物应用中,经常需要比较两个(或多个)不同生物体的DNA, 例如:某种生物的DNA可能为S1=ACCGGTCGAGTGCGCGGAAGCCGGCCGAA, 另一种生物的DNA可能为S2=GTCGTT ...
- C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解
版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...
- Dynamic Programming | Set 4 (Longest Common Subsequence)
首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", ...
随机推荐
- 关于Unity中的屏幕适配
一.Game视图的屏幕分辨率可以先自定义添加,供以后选择,以下是手游经常用到的分辨率: 1.1136X640,iPhone5 2.1920X1080,横屏,主流游戏都是这个分辨率 3.1080X192 ...
- 中国餐馆过程(Chinese restaurant process)
也就是说假设空桌子有a0个人,然后顾客选择桌子的概率和桌子上人数成正比. 性质: 改变用户的排列方式,桌子的排列方式,概率不变换.
- DirectoryEntry 账户启动与停用 以及创建账户等
启动账户: DirectoryEntry usr = new DirectoryEntry("LDAP://CN=New User,CN=users,DC=fabrikam,DC=com&q ...
- CentOS查看何人何时登陆用户
使用linux 的last命令: last命令列出的是/var/log 目录下的wtmp文件内容,这个文件存的是二进制内容,不可以直接用vi等文本边界软件打开.这样即使是root用户也不可能随随便便的 ...
- 关于C++中using namespace std
原文链接:http://www.kuqin.com/language/20080107/3532.html <iostream>和<iostream.h>是不一样,前者没有后缀 ...
- tomcat+nginx反向代理(实现一个服务器,一个ip共用80端口)
http://blog.csdn.net/sinat_33388558/article/details/54837051 http://www.cnblogs.com/zhouqinxiong/p/5 ...
- Hibernate学习(1):查询demo
1.数据库(mysql)创建脚本 DROP TABLE IF EXISTS role; CREATE TABLE IF NOT EXISTS `role`( `id` ) NOT NULL AUTO_ ...
- 【ML】scikit-learn-book
http://nbviewer.ipython.org/github/gmonce/scikit-learn-book/tree/master/
- centos7下配置免密码登录
主机master ,slaver1,slaver2 1.每台主机都执行 ssh-keygen -t rsa 然后一直回车 2.操作master.master生成公钥 放入authorized_keys ...
- idea 新建项目 文件名都是红色的处理办法
原因是当前的project用了版本控制器 所以这个project下面所有的项目都加入版本控制器里了,所以项目文件和名称都是红色的 简单文字叙述解决办法 file-->settings--> ...