给出两个字符串(不长于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的更多相关文章

  1. LeetCode-Repeated DNA Sequences (位图算法减少内存)

    Repeated DNA Sequences All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, ...

  2. lc面试准备:Repeated DNA Sequences

    1 题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...

  3. LeetCode 187. 重复的DNA序列(Repeated DNA Sequences)

    187. 重复的DNA序列 187. Repeated DNA Sequences 题目描述 All DNA is composed of a series of nucleotides abbrev ...

  4. [LeetCode] Repeated DNA Sequences 求重复的DNA序列

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  5. [Leetcode] Repeated DNA Sequences

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  6. 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 ...

  7. 【leetcode】Repeated DNA Sequences(middle)★

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  8. LeetCode() Repeated DNA Sequences 看的非常的过瘾!

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  9. Repeated DNA Sequences

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

随机推荐

  1. PHP面向对象(OOP):__call()处理调用错误

    在程序开发中,如果在使用对象调用对象内部方法时候,调用的这个方法不存在那么程序就会出错,然后程序退出不能继续执行.那么可不可以在程序调用对象内部 不存在的方法时,提示我们调用的方法及使用的参数不存在, ...

  2. python连接mysql之pymysql模块

    以下demo均以python2中的mysqldb模块 一.插入数据 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import MySQLdb    conn = MyS ...

  3. smali文件语法参考

    Dalvik opcodes Author: Gabor Paller Vx values in the table denote a Dalvik register. Depending on th ...

  4. spring mvc ModelAndView 404的原因

    在使用ModelAndView时不要导入 import org.springframework.web.portlet.ModelAndView; 而要导入以下这个包 import org.sprin ...

  5. IOS 点击空白处隐藏键盘的几种方法

    IOS 点击空白处隐藏键盘的几种方法     IOS7 点击空白处隐藏键盘的几种方法   IOS开发中经常要用到输入框,默认情况下点击输入框就会弹出键盘,但是必须要实现输入框return的委托方法才能 ...

  6. Zephyr-MQTT

    Zephyr OS 支持MQTT协议,其源码目录在: # cd /zephyr-/samples/net/paho_mqtt_clients/publisher/ # cd /zephyr-1.5.0 ...

  7. vmware中ubuntu更新内核后无法进入桌面,鼠标“漂移”滑动

    问题背景: 我机子上是在vmware下安装了ubuntu12.04,今天正在ubuntu下工作,结果提示内核有更新,手贱的就点了个OK,开始更新,更新完重启.结果,问题来了,刚开始系统启动,进入系统登 ...

  8. 会声会影X6-高级运动等效果的练习实践-与您分享...

          视频片说明:我在学习X6的视频教程后,做了针对性练习与实 践,我所用的素材取于网络世界-百度下载,视频中的效果有,高级运动;平移缩放,分屏效果,<运用:关键帧,缩放,旋转,加相框,倒 ...

  9. 模拟键盘发送文字(使用SendInput函数)

    嗯...老生常谈的话题, 不过系统的总结了一下, 找了个相对简单的实现方式, 可以方便的发送任何文字 参考另一片文章: http://www.cnblogs.com/-clq/archive/2011 ...

  10. OpenCV 2.4.3在VS2010上的应用

    一.下载和安装:    1.OpenCV 2.4.3下载:http://www.opencv.org.cn/index.php/Download#Version_2.4.3    2.下载完成后,解压 ...