最长公共字序列.cpp
<span style="color:#993399;">/*
By yuan
2014/6/21
At nwpu.xf 1041.最长公共子序列
时限:1000ms 内存限制:200000K 总时限:3000ms
描写叙述
一个给定序列的子序列是在该序列中删去若干元素后得到的序列。确切地说,若给定序列X=<x1, x2,…, xm>,则还有一序列Z=<z1, z2,…, zk>是X的子序列是指存在一个严格递增的下标序列 <i1, i2,…, ik>。使得对于全部j=1,2,…,k有: Xij = Zj 假设一个序列S即是A的子序列又是B的子序列,则称S是A、B的公共子序列。
求A、B全部公共子序列中最长的序列的长度。 输入
输入共两行,每行一个由字母和数字组成的字符串,代表序列A、B。A、B的长度不超过200个字符。 输出
一个整数。表示最长各个子序列的长度。
格式:printf("%d\n"); 输入例子
programming
contest 输出例子
2 提示 来源
*/
#include<iostream>
#include<stdio.h> #include<stdlib.h>
#include<string.h>
using namespace std;
char a[201],b[201];
int sum[201][201];
int la,lb;
void slove()
{int i,j;
for(i=1;i<=la;i++)
for(j=1;j<=lb;j++)
{
if(a[i]==b[j])
sum[i][j]=sum[i-1][j-1]+1;
else if(sum[i-1][j]>=sum[i][j-1])
sum[i][j]=sum[i-1][j];
else
sum[i][j]=sum[i][j-1];
}
} int main()
{int i,j;
memset(a,'0',201);
memset(b,'0',201);
memset(sum,0,201*201);
for(i=1;;i++){
scanf("%c",&a[i]);
if(a[i]=='\n') break;}
la=i-1;
for(j=1;;j++){
scanf("%c",&b[j]);
if(b[j]=='\n') break;}
lb=j-1;
slove();
cout<<sum[la][lb]<<endl;
return 0;
}</span>
最长公共字序列.cpp的更多相关文章
- hdu 1243 反恐训练营 最长公共字序列
此题的题意很明确,就是求最长公共子序列: #include<iostream> #include<algorithm> #include<cstdio> #incl ...
- URAL 1517 Freedom of Choice(后缀数组,最长公共字串)
题目 输出最长公共字串 #define maxn 200010 int wa[maxn],wb[maxn],wv[maxn],ws[maxn]; int cmp(int *r,int a,int b, ...
- (字符串)最长公共字串(Longest-Common-SubString,LCS)
题目: 给定两个字符串X,Y,求二者最长的公共子串,例如X=[aaaba],Y=[abaa].二者的最长公共子串为[aba],长度为3. 子序列是不要求连续的,字串必须是连续的. 思路与代码: 1.简 ...
- HDU 5904 LCIS (最长公共上升序列)
传送门 Description Alex has two sequences a1,a2,...,an and b1,b2,...,bm. He wants find a longest common ...
- 最长公共子序列与最长公共字串 (dp)转载http://blog.csdn.net/u012102306/article/details/53184446
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- poj 3080 kmp求解多个字符串的最长公共字串,(数据小,有点小暴力 16ms)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14113 Accepted: 6260 Descr ...
- java_基础知识_字符串练习题_计算两个字符串的最长公共字串长度
package tek; Java算法——求出两个字符串的最长公共字符串 /** * @Title: 问题:有两个字符串str1和str2,求出两个字符串中最长公共字符串. * @author 匹夫( ...
- HDU 1423 最长公共字串+上升子序列
http://acm.hdu.edu.cn/showproblem.php?pid=1423 在前一道题的基础上多了一次筛选 要选出一个最长的递增数列 lower_bound()函数很好用,二分搜索找 ...
- 最长公共字串(LCS)最长连续公共字串(LCCS)
链接1:http://blog.csdn.net/x_xiaoge/article/details/7376220 链接2:http://blog.csdn.net/x_xiaoge/article/ ...
随机推荐
- Android ScrollView嵌套GridView导致GridView只显示一行item
Android ScrollView嵌套GridView导致GridView只显示一行item Android ScrollView在嵌套GridView时候,会导致一个问题发生:GridView只显 ...
- BZOJ 1009 [HNOI2008]GT考试 ——矩阵乘法 KMP
先用KMP处理所有的转移,或者直接暴力也可以. 然后矩阵快速幂即可. #include <cstdio> #include <cstring> #include <ios ...
- poj1930 数论
Dead Fraction Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1258 Accepted: 379 Desc ...
- 使用腾讯互动直播 遇到的坑 'GLIBC_2.14' not found 问题解决
第一.查看系统glibc版本库 strings /lib64/libc.so.6 |grep GLIBC_ 这里我们可以看到系统中最新的版本是2.12,这里我们升级2.14. 第二.下载和安装glib ...
- poj 1031 多边形对点(向周围发射光线)的覆盖
Fence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3018 Accepted: 1010 Description ...
- POJ3345 Bribing FIPA
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5021 Accepted: 1574 Description There ...
- indexedDB 增删改查
/** * 打开数据库 */ export function openDB() { return new Promise((resolve, reject) => { let indexedDB ...
- 通过Nginx 的反向代理来加强kibana的访问安全
https://blog.csdn.net/choelea/article/details/57406086
- Highcharts 动态制作3D柱状图
学习参考菜鸟网站:http://www.runoob.com/highcharts/highcharts-tutorial.html 我是通过后端返回设备数据,进行前端出图,效果如下: 代码如下: d ...
- 牛客练习赛1 矩阵 字符串二维hash+二分
题目 https://ac.nowcoder.com/acm/contest/2?&headNav=www#question 解析 我们对矩阵进行二维hash,所以每个子矩阵都有一个额hash ...