Back to Edit Distance(LCS + LIS)
Given 2 permutations of integers from 1 to N, you need to find the minimum number of operations necessary to change both or any one of them in such a way that they become exactly same. Here only two operations are allowed: either you can delete an integer from any position or you can insert an integer into any position, but replacing one integer by another one is not allowed. Say, N = 5 and the permutations are {1, 3, 5, 4, 2} and {1, 5, 4, 3, 2}. Then we need just 2 operations: we need to delete 3 from the 2nd position and insert it in the 4th position of the first permutation, or we can delete 3 from both the permutations, which also needs two operations.
Input
First line of the input contains a positive integer T (T ≤ 40). Each of the following T cases contains 3 lines for each case: the 1st line contains a single integer N (1 ≤ N ≤ 200, 000) and the next two lines contain the two permutations of the integers.
Output
For each case, print a line of the form ‘Case < x >: < y >’, where x is the case number and y is the number of operations necessary to covert the 1st permutation to the 2nd permutation.
Sample Input
2 5 1 3 5
4 2 1 5 4
3 2 4 1 2
4 3 3 4 2 1
Sample Output
Case 1: 2
Case 2: 6
- #include<bits/stdc++.h>
- using namespace std;
- const int M = 2e5 + 10 , inf = 0x3f3f3f3f;
- int n ;
- int orm[M] ;
- int a[M] ;
- int Top[M] ;
- int judge (int x) {
- int l = 0 , r = n ;
- int ret = l ;
- while (l <= r) {
- int mid = l+r >> 1 ;
- if (x > Top[mid]) {
- ret = mid ;
- l = mid+1 ;
- }
- else r = mid-1 ;
- }
- Top[ret+1] = min (Top[ret+1] , x) ;
- return ret+1 ;
- }
- int LIS () {
- int ans = 0 ;
- for (int i = 1 ; i <= n ; i ++) {
- ans = max (ans , judge (a[i])) ;
- }
- return ans ;
- }
- int main () {
- int T ;
- scanf ("%d" , &T ) ;
- for (int cas = 1 ; cas <= T ; cas ++) {
- scanf ("%d" , &n) ;
- for (int i = 1 ; i <= n ; i ++) {
- int x ;
- scanf ("%d" , &x) ;
- orm[x] = i ;
- Top[i] = inf ;
- }
- for (int j = 1 ; j <= n ; j ++) {
- int x ;
- scanf ("%d" , &x) ;
- a[j] = orm[x] ;
- }
- printf ("Case %d: %d\n" , cas , (n-LIS ())*2) ;
- }
- return 0 ;
- }
要灵活运用他是一个1~n的排列。
然后你就能把lcs变成lis了。
Back to Edit Distance(LCS + LIS)的更多相关文章
- CJOJ 1070 【Uva】嵌套矩形(动态规划 图论)
CJOJ 1070 [Uva]嵌套矩形(动态规划 图论) Description 有 n 个矩形,每个矩形可以用两个整数 a, b 描述,表示它的长和宽.矩形 X(a, b) 可以嵌套在矩形 Y(c, ...
- [LeetCode] 72. Edit Distance(最短编辑距离)
传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...
- Minimum edit distance(levenshtein distance)(最小编辑距离)初探
最小编辑距离的定义:编辑距离(Edit Distance),又称Levenshtein距离.是指两个字串之间,由一个转成还有一个所需的最少编辑操作次数.许可的编辑操作包含将一个字符替换成还有一个字符. ...
- [LeetCode] Edit Distance(很好的DP)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- [leetcode72]Edit Distance(dp)
题目链接:https://leetcode.com/problems/edit-distance/ 题意:求字符串的最短编辑距离,就是有三个操作,插入一个字符.删除一个字符.修改一个字符,最终让两个字 ...
- [HAOI2010]最长公共子序列(LCS+dp计数)
字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x0,x1,…,xm-1”,序列Y=“y0,y1,…,yk-1”是X ...
- 题解报告:poj 2689 Prime Distance(区间素数筛)
Description The branch of mathematics called number theory is about properties of numbers. One of th ...
- 71.Edit Distance(编辑距离)
Level: Hard 题目描述: Given two words word1 and word2, find the minimum number of operations required ...
- HDU5812 Distance(枚举 + 分解因子)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5812 Description In number theory, a prime is a ...
随机推荐
- 数据结构作业——max_and_min(栈)
Description TonyY 最近喜欢上了数学,今天他研究一个只有加号和乘号,运算数为整数, 大小在 1-9 之间的表达式,你可以任意地往里加括号,如何让表达式的值最大或 者最小? Input ...
- Markdown的使用简介
以前有摘抄过,然而onenote速度感人,现在又主要用Linux,所以在这里备份一下,好方便用 Linux下推荐remakeble软件,或者直接sublime text,再或者vim,反正我不会ema ...
- JSP+Servlet+javabean+oracle实现页面多条件模糊查询
之前写过一篇JSP+Servlet+javabean+mysql实现页面多条件模糊查询 使用的是mysql进行的分页查询,mysql用limit控制,而oracle则是用rownum,今天第一次写or ...
- JSP表单提交中文乱码解决方案
分2种提交方式,解决方案不同: 1.form表单提交方式为get 乱码: 解决方案: 因为get方法是参数在URL中显示,因为tomcat的URL编码默认是:IOS-8859-1所以要么改tomcat ...
- Java 序列化Serializable接口
1 什么是序列化和反序列化 Serialization(序列化)是一种将对象以一连串的字节描述的过程:反序列化deserialization是一种将这些字节重建成一个对象的过程. 2 什么情况下需要 ...
- React Native 开发之 (05) flexbox布局
一 flexbox布局 1 flex布局 flexbox是ReactNative 应用开发中必不可少的内容,也是最常用的内容. 传统的页面布局是基于盒子模型,依赖定位属性,流动属性和显示属性来解决. ...
- VGA 视频输出
VGA Video Output by Nathan Ickes Introduction VGA is a high-resolution video standard used mostly fo ...
- HTTPS背后的加密算法
当你在浏览器的地址栏上输入https开头的网址后,浏览器和服务器之间会在接下来的几百毫秒内进行大量的通信.InfoQ的这篇文章对此有非常详细的描述.这些复杂的步骤的第一步,就是浏览器与服务器之间协商一 ...
- Mysql事务,并发问题,锁机制
.什么是事务 事务是一条或多条数据库操作语句的组合,具备ACID,4个特点. 原子性:要不全部成功,要不全部撤销 隔离性:事务之间相互独立,互不干扰 一致性:数据库正确地改变状态后,数据库的一致性约束 ...
- Jquery实现textarea根据文本内容自适应高度
本文给大家分享的是Jquery实现textarea根据文本内容自适应高度,这些在平时的项目中挺实用的,所以抽空封装了一个文本框根据输入内容自适应高度的插件,这里推荐给小伙伴们. autoTextare ...