SPOJ 3048 - DNA Sequences LCS
给出两个字符串(不长于1000),求最长公共子序列,要求:从每个串中取必须取连续k (1<=k<=100)个数
【LCS】一开始自己想用DP加一维[len]用来表示当前已经取了连续len个值,但是1000*1000*100肯定超时,而且这道题的时限779ms是什么鬼
然后想求LCS有没有像LIS一样优化到nlogn的算法,百度一下,还真有【戳这里跳转】,但是基于这个算法来求这道题始终没有什么思路。
还是回到原点设dp[i][j]为第一个字符串到第i位,第二个字符串到第j位,的最大匹配数
不能匹配的时候:dp[i][j]=max( dp[i-1][j] , dp[i][j-1] )
可以匹配的时候:dp[i][j]=max( dp[i][j] , dp[i-p][j-p]+p ) 其中p>=k
【代码链接】
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<queue>
#include<string>
#include<sstream>
#define eps 1e-9
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define FOR(i,j,k) for(int i=j;i<=k;i++)
#define MAXN 1005
#define MAXM 40005
#define INF 0x3fffffff
using namespace std;
typedef long long LL;
int i,j,k,n,m,x,y,T,ans,big,cas,num,len;
bool flag;
char a[],b[];
int dp[][],lena,lenb,p;
int main()
{
while(scanf("%d",&k),k)
{
scanf("%s",a);lena=strlen(a);
scanf("%s",b);lenb=strlen(b);
memset(dp,,sizeof(dp));
for (i=;i<=lena;i++)
{
for (j=;j<=lenb;j++)
{
dp[i][j]=max(dp[i][j-],dp[i-][j]);
for (p=; i-p>= && j-p>= && a[i-p]==b[j-p] ; p++)
{
if (p>=k)
{
dp[i][j]=max(dp[i][j],dp[i-p][j-p]+p);
}
}
}
}
printf("%d\n",dp[lena][lenb]);
}
return ;
}
SPOJ 3048 - DNA Sequences LCS的更多相关文章
- LeetCode-Repeated DNA Sequences (位图算法减少内存)
Repeated DNA Sequences All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, ...
- lc面试准备:Repeated DNA Sequences
1 题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...
- LeetCode 187. 重复的DNA序列(Repeated DNA Sequences)
187. 重复的DNA序列 187. Repeated DNA Sequences 题目描述 All DNA is composed of a series of nucleotides abbrev ...
- [LeetCode] Repeated DNA Sequences 求重复的DNA序列
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- [Leetcode] Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- leetcode 187. Repeated DNA Sequences 求重复的DNA串 ---------- java
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 【leetcode】Repeated DNA Sequences(middle)★
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- LeetCode() Repeated DNA Sequences 看的非常的过瘾!
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
随机推荐
- PHP学习之中数组--创建数组【1】
在PHP中数组的定义有三种写法,分别是: <?php //第一种方式是用关键字 array来创建的 $username = array("demo1","demo2 ...
- Java线程生命模型
一. 线程状态类型1. 新建状态(New):新创建了一个线程对象.2. 就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中,变得可运 ...
- 如何使用json在前后台进行数据传输
上一篇博客写到用javascript生成多组文本,可以让数据的输入不受显示,现在我们需要把这些输入写入数据库,这里就用到json传入. 首先,我们来写一下后台如何生成要传输的数据 function g ...
- 线性链表的双向链表——java实现
.线性表链式存储结构:将采用一组地址的任意的存储单元存放线性表中的数据元素. 链表又可分为: 单链表:每个节点只保留一个引用,该引用指向当前节点的下一个节点,没有引用指向头结点,尾节点的next引用为 ...
- 【HDOJ】2267 How Many People Can Survive
BFS. #include <iostream> #include <cstdio> #include <cstring> #include <queue&g ...
- Git批量修改提交历史
有些时候我们可能需要批量修改提交历史,当然了,最近一次的提交历史很简单我们可以利用 git commit --amend 来进行最近一次提交的修改,如果你此时想要更新作者提交时间等也可以在amend之 ...
- cscope使用
[[]][]再加上][一共是 4 个在段落(对于 C 来讲就是函数)间跳转的命令. 总结是:1,相同就跳到函数的开头:(如果都是左括号或者都是右括号),不同就跳到函数的结尾: { 和 } 用来 ...
- 【gm】
gm : GraphicsMagick for node.js aheckmann/gm imgAreaSelect 图片剪裁 apt-get install imagemagick 执行conver ...
- 【索引】Android相关的
Eclipse开发环境 JDK的下载和安装:http://www.cnblogs.com/duxiuxing/p/4771901.html Android SDK的下载和安装:http://www.c ...
- Solr开发参考文档(转)
Solr开发文档 Solr 是一种可供企业使用的.基于 Lucene 的搜索服务器,它支持层面搜索.命中醒目显示和多种输出格式.在这篇文章中,将介绍 Solr 并展示如何轻松地将其表现优异的全文本搜索 ...