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. python_10(模块与包)

    第1章 模块 1.1 模块的种类 1.2 定义 1.3 作用 1.4 导入及使用 1.4.1 import 1.4.2 测试一: 1.4.3 测试二: 1.4.4 测试三: 1.4.5 小结 1.4. ...

  2. Linux--NiaoGe-Service-01

    安装环境介绍 CentOS 6.9_x86_64 我们选择的是基本安装,也即“Basic Server”. 安装完成后重启来到(runlevel 3)纯文本界面. 例题 批量创建账号:假设有5个账号x ...

  3. c#很好用的定时器Quartz--含附件

    1.引用附件中的两个DLL 2.创建类 public class QuartzJob:IStatefulJob { private static ISchedulerFactory factory = ...

  4. Json和序列化总结

    一.序言 遇到问题,就经常逛园,不知你是否曾有,曾经遇到的问题,或者在园子里看到问题的方案,过一段时间,有可能还会摔跤,哈哈...大神请勿喷,小弟记忆不太好,还过来找资料,如果自己写把问题或某个知识点 ...

  5. js调用本地程序

    前几天,做项目时候用到js调用本地的程序,找了好多资料,一种是写入注册表,一种是写一个浏览器插件,相对来说,写一个注册表更简单一点,因为需求很紧.下面就是我的总结,希望可以对你们有所帮助,具体从哪里找 ...

  6. VB.NET入门 ANDALSO 和OrElse 之于 AND,OR

    Module Module1 Sub Main() Dim x As Integer = 8, y As Integer = 5, z As Integer = 3 Console.WriteLine ...

  7. Java基础50题test3—水仙花数

    水仙花数 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例 如:153 是一个"水仙花数", ...

  8. 使用预定义的action值启动系统应用

    1.启动浏览器 Intent intent = new Intent(); intent.setAction(Intent.ACTION_WEB_SEARCH); //可以传一个搜索关键字,会直接显示 ...

  9. jQuery选择器之可见性选择器

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...

  10. VS Code使用技巧整理

    转自:https://blog.csdn.net/u011127019/article/details/58586129 https://blog.csdn.net/sgdd123/article/d ...