See LCS again

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
描述

There are A, B two sequences, the number of elements in the sequence is n、m;

Each element in the sequence are different and less than 100000.

Calculate the length of the longest common subsequence of A and B.

输入
The input has multicases.Each test case consists of three lines;

The first line consist two integers n, m (1 < = n, m < = 100000);

The second line with n integers, expressed sequence A;

The third line with m integers, expressed sequence B;
输出
For each set of test cases, output the length of the longest common subsequence of A and B, in a single line.
样例输入
5 4
1 2 6 5 4
1 3 5 4
样例输出
3
上传者
TC_胡仁东

东哥的题,,TC元老级人物,请收下15级菜鸡的膝盖。

很裸的求LCS。但基于数据大,所以超时的做法不用考虑了。看提交纪律很多MLE的,于是用滚动数组。。WA了。于是在网上找了一种很暴力钻数据空子的做法。详见:http://karsbin.blog.51cto.com/1156716/966387

其实这种做法以前寒假学LCS的时候在网上看到过,当时和小田说了一下,我们都很震惊。但后来被他所举的例子也就是上面博客中提到的例子退化的LCS所推翻了。基于数据水的前提下是可以试试的。一下摘自上面那位大神的博客:

这里也可将其转化为最长递增子序列问题。

举例说明:

A:abdba

B:dbaaba

则1:先顺序扫描A串,取其在B串的所有位置:

2:a(2,3,5) b(1,4) d(0)。

3:用每个字母的反序列替换,则最终的最长严格递增子序列的长度即为解。

替换结果:532 41 0 41 532

最大长度为3.

简单说明:上面的序列和最长公共子串是等价的。

对于一个满足最长严格递增子序列的序列,该序列必对应一个匹配的子串。

反序是为了在递增子串中,每个字母对应的序列最多只有一个被选出。

反证法可知不存在更大的公共子串,因为如果存在,则求得的最长递增子序列不是最长的,矛盾。

最长递增子序列可在O(NLogN)的时间内算出。

配上代码:

const int N=1e5+7;
int c[N],d[N],a[N],b[N],v[N];
int find(int x,int *a,int len)
{
int l=0,r=len;
while(l<=r)
{
int mid=(l+r)/2;
if(a[mid]==x) return mid;
if(a[mid]<x) l=mid+1;
else r=mid-1;
}
return l;
}
void dp(int *a,int k)
{
int len=0;
int b[N];
b[1]=a[0];
if(k) len=1;
for(int i=1;i<k;i++)
{
if(b[len]<a[i])
b[++len]=a[i];
else
{
int pos=find(a[i],b,len);
b[pos]=a[i];
}
}
printf("%d\n",len);
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(v,0,sizeof(v));
vector<int>q[N];
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
v[a[i]]=1;
}
for(int i=0; i<m; i++)
{
scanf("%d",&b[i]);
if(v[b[i]]) q[b[i]].push_back(i);
}
int k=0;
for(int i=0; i<n; i++)
if(!q[a[i]].empty())//其实这里就钻了空子,如果一万个1和一万个1超时是必然的。
{
for(int j=q[a[i]].size()-1; j>=0; j--)
c[k++]=q[a[i]][j];
}
//for(int i=0;i<k;i++) printf("%d%c",c[i],i==k-1?'\n':' ');
dp(c,k);
}
return 0;
}

以上是本菜鸡的一点想法,严格来说不够严谨,单纯为了提高题量。。这样是很不好的。

如果路过的大牛有更好的思路,欢迎提出。

NYOJ760-See LCS again,有技巧的暴力!的更多相关文章

  1. HDU 6351 (带技巧的暴力)

    题意:给定一个数,和一个最多交换次数k,问在不超过k次操作的情况,问可以得到的最大值和最小值是多少? 个人解题的艰辛路程 , 开始是想到了暴力枚举的可能 , 打出来发现在判断枚举的数组与原来数组交换了 ...

  2. Divisibility by 25 CodeForces - 988E (技巧的暴力)

    You are given an integer nn from 11 to 10181018 without leading zeroes. In one move you can swap any ...

  3. hdu 1006 Tick and Tick 有技巧的暴力

    Tick and Tick Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. Codeforces A. Playlist(暴力剪枝)

    题目描述: Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  5. TopCoder比赛总结表

    TopCoder                        250                              500                                 ...

  6. LeetCode-3LongestSubstringWithoutRepeatingCharacters(C#)

    # 题目 3. Longest Substring Without Repeating Characters Given a string, find the length of the longes ...

  7. 2015弱校联盟(1) - E. Rectangle

    E. Rectangle Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class name ...

  8. HDU 4358 莫队算法+dfs序+离散化

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others)T ...

  9. Educational Codeforces Round 44 (Rated for Div. 2)

    题目链接:https://codeforces.com/contest/985 ’A.Chess Placing 题意:给了一维的一个棋盘,共有n(n必为偶数)个格子.棋盘上是黑白相间的.现在棋盘上有 ...

随机推荐

  1. Oracle及其相关软件历史版本下载地址

    https://edelivery.oracle.com/osdc/faces/Home.jspx 打开上面这个链接,输入自己或可用的帐号即可. 搜索到自己想要下载的软件后,点击,软件会添加到购物车中 ...

  2. php,json数据传输(无刷新)

    废话不说直接上关键代码: js代码: <script language="javascript"> $(".login").live('click' ...

  3. 为localhost添加https

    1.按照https://stackoverflow.com/a/7184031/4619958来做 其中,CommonName填写localhost 2.在ssl.conf里头加上 <Direc ...

  4. poj2385 Apple Catching

    思路: 简单dp. 实现: #include <iostream> #include <cstdio> #include <cstring> using names ...

  5. jdk1.8新日期时间类(DateTime、LocalDateTime)demo代码

    //获取当前时间 LocalDateTime d0 = LocalDateTime.now(); System.out.println(DataConvertUtil.localDateTimeToS ...

  6. python中的格式化字符

    python中的格式化字符在python中我们会遇到一个问题,问题是如何输出格式化的字符串.我们经常会输出类似'亲爱的xxx你好!你xx月的话费是xx,余额是xx'之类的字符串,而xxx的内容都是根据 ...

  7. PHP Deprecated: Function split() is deprecated in /var/www/html/cacti/cmd.php on line 61

    [root@localhost cacti]# php cmd.php PHP Deprecated: Function split() is deprecated in /var/www/html/ ...

  8. laravel关联用户

    参考文档:模型关联-反向关联 belongsToor 模型层 app/Post.php public function user() { return $this->belongsTo('\Ap ...

  9. java访问数据库步骤详解

    eg1: public static void main(String[] args) throws ClassNotFoundException, SQLException { //第一步:加载JD ...

  10. Linux 从源码编译安装 Nginx

    Nginx 是一个高性能的 HTTP 和 反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器.Nginx 编译安装比较简单,难点在于配置.下面是 Nignx 0.8.54 编译安装和简 ...