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了。

时间: 2024-10-24 05:42:50

Back to Edit Distance(LCS + LIS)的相关文章

LeetCode:Edit Distance(字符串编辑距离DP)

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a character b) Delete a character c) Repla

[LeetCode] Edit Distance(很好的DP)

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a character b) Delete a character c) Repla

HDU 1503 Advanced Fruits (LCS,DP)

题意:给你两字符串s1,s2,用最短的字符串表示他们(公共字串输出一次). Sample Input apple peach ananas banana pear peach Sample Output appleach bananas pearch dp[i][j] : 第一个字符串的前 i 个 ,和第二个字符串的前 j 个最短组合的长度 . pre[i][j] : 第一个字符串的第 i 个 ,和第二个字符串的第 j 个字符的状态. #include<cstdio> #include<

BZOJ 1264 基因匹配Match(LCS转化LIS)

题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1264 题意:给出两个数列,每个数列的长度为5n,其中1-n每个数字各出现5次.求两个数列的最长公共子列. 思 路:首先找出每个数字在第二个数列中出现的位置,对于第一个数列构造出一个新的数列,每个数用这个数在第二个数列中出现的5个位置代替.这样求最长上升子 列.注意的是,在求到每个数(这里指原第一个数列中的每个数)为止的LIS时,替换后的5个数字要先求大的..不清楚的看代码. int a

Minimum edit distance(levenshtein distance)(最小编辑距离)初探

最小编辑距离的定义:编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符. 例如将kitten一字转成sitting: sitten(k→s) sittin(e→i) sitting(→g) 俄罗斯科学家Vladimir Levenshtein在1965年提出这个概念. Thewords `computer' and `commuter' are

[leetcode72]Edit Distance(dp)

题目链接:https://leetcode.com/problems/edit-distance/ 题意:求字符串的最短编辑距离,就是有三个操作,插入一个字符.删除一个字符.修改一个字符,最终让两个字符串相等. DP,定义两个字符串a和b,dp(i,j)为截至ai-1和bj-1时的最短编辑距离. 当ai-1=bi-1的时候,有dp(i,j)=min(dp(i,j),dp(i-1,j-1)),对应不做任何操作: 不相等的时候会有dp(i,j)=min(dp(i,j),dp(i-1,j-1)+1),

WOJ 1047 LCS problem (LCS 算法总结 )

http://acm.whu.edu.cn/land/problem/detail?problem_id=1047 Description Recently, Flymouse reads a book about Algorithm and Data Structure. The book reads: there are two types of LCS Problems. One is Longest Common Subsequence problem. By the way of Dy

HDU 1503 Advanced Fruits(LCS+记录路径)

http://acm.hdu.edu.cn/showproblem.php?pid=1503 题意: 给出两个串,现在要确定一个尽量短的串,使得该串的子串包含了题目所给的两个串. 思路: 这道题目就是要我们求LCS,记录好路径就好. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<sstream> 6 #inc

POJ-2689-Prime Distance(素数区间筛法)

链接: https://vjudge.net/problem/POJ-2689 题意: The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A