See LCS again

2024-06-08 18:38
文章标签 lcs see

本文主要是介绍See LCS again,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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_胡仁东

这个题关键是将LCS问题转化为最长上升子序列问题 (算法时间复杂度nlogn的方法)二分查找 详细见另一篇博客
AC代码如下:
#include<iostream>
#include<string.h>
#define N 100001
using namespace std;
int D[N];
int a[N],b[N];
int binarysearch(int low,int high,int m)//二分查找法
{int mid;mid=(low+high)/2;while(low<=high){if(D[mid]<m&&D[mid+1]>=m) return mid;else if(D[mid]<m)low=mid+1;else high=mid-1;mid=(low+high)/2;}  return mid;
}    
int main()
{int n,m;int i,j;int x;while(cin>>n>>m){memset(a,0,sizeof(a));memset(b,0,sizeof(b));for(i=1;i<=n;i++)//按顺序记录a串中各字符的位置{cin>>x;a[x]=i;}j=1;for(i=1;i<=m;i++){cin>>x;if(a[x])         //只保存a,b串公有字符b[j++]=a[x];//将问题转化成了最长递增子序列a[x]是x在a串的位置}int k,len;//memset(D,0,sizeof(D));D[1]=b[1];len=1;n=j-1;for(i=2;i<=n;i++){if(b[i]>D[len]){len++;D[len]=b[i];}else{j=binarysearch(1,len,b[i]);k=j+1;D[k]=b[i];}  }   cout<<len<<endl;  }return 0;
}



这篇关于See LCS again的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1042986

相关文章

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

uva 10066 LCS

题意: 最长公共子序列。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include

编译时出现错误 -- clang: error: linker command failed with exit code 1 (use -v to see invocation)

出现这个错误的原因有多种,常见的是因为某些文件的缺失或者是文件的重复导致的。 这类错误查看的关键在于其上一行的文字。 对于文件缺少而导致错误的情况: 例如上图中的示例,其上一行文字为 ld:library not found for -lrxl,可以看出是缺失了某一文件而导致的错误,这行文字中的最后“ -lrxl ”:-l 代表着其前缀是“lib”,连着后面的 rxl,其名称为 libr

NLP文本相似度之LCS

基础 LCS(Longest Common Subsequence)通常指的是最长公共子序列,区别最长公共字串(Longest Common Substring)。我们先从子序列的定义理解: 一个序列S任意删除若干个字符得到新的序列T,则T叫做S的子序列。 子序列和子串的一个很大的不同点是,子序列不要求连接,而子串要求连接。 两个序列X和Y的公共子序列中,长度最长的那个,定义为X和Y

最长公共子串(LCS)

(1) 找出两个字符串的最长公共子串 题目:输入两个字符串,找出两个字符串中最长的公共子串。 找两个字符串的最长公共子串,这个子串要求在原字符串中是连续的。 #include<iostream>#include<string>using namespace std;int main(){string s1 = "GCCCTAGCCAGDE";string s2 = "GCGCCAGT

LCS转为LIS

(1) 先将所有数列排序。     第一维由小到大,当第一维相等时,第二维由大到小。   (排序规则亦可改成以第二个维度为主,最后则是找第一个维度的1DLIS。)       a     a    a     b     b    a     a     a    c     d   (0,6) (0,3) (0,2) (1,4) (1,1)(2,6) (2,3) (2,2) (3,5) (4,

【UVA10405】【裸LCS】

坑点在于输入有空格 #include <iostream>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <string>#include <cstdlib>#include <c

Java Doc -- `{@link}` 和 `@see` 的用法

前言 Java Doc 是一种为 Java 代码生成文档的标准工具。它通过特殊的注释格式帮助开发者记录代码的功能、参数、返回值等信息,从而生成易于阅读的 HTML 文档。本文将详细介绍如何使用 {@link} 和 @see 标签来增强 Javadoc 的链接功能。 1. 什么是 {@link}? {@link} 标签用于在 Javadoc 注释中创建指向其他文档元素(如类、方法、字段等)的链

lightoj1033 - Generating Palindromes (LCS)

题意:给你一个字符串,至少需要添加多少字符可以使得它变成一个回文串. 思路 :设串S的反串为S‘那么strlen(S) - LCS(S, S')就是本问题的答案. 如: S(原串)       A   b  3  b  d S1(倒序串)   d   b  3  b  A LCS                   b  3  b         所以,有3个字符已经配对,不用添加

LIS(最长递增子序列)和LCS(最长公共子序列)的总结

LIS(最长递增子序列)和LCS(最长公共子序列)的总结 最长公共子序列(LCS):O(n^2) 两个for循环让两个字符串按位的匹配:i in range(1, len1) j in range(1, len2) s1[i - 1] == s2[j - 1], dp[i][j] = dp[i - 1][j -1] + 1; s1[i - 1] != s2[j - 1], dp[