UVA - 10635 最长公共子序列

input

n,p,q 2<=n<=250 1<=p,q<=n*n

1 a1 a2 a3 ... ap  1<ai<n*n,ai!=aj

1 b1 b2 b3 ... bq  1<bi<n*n,bi!=bj

output

最长公共子序列个数

做法:将b数组中的数变为a数组中数的下标,a中不存在的数可以去掉,然后求LIS即可

#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
#define MAX 63000
#define LL long long
using namespace std;
int a[MAX],b,len[MAX],idx[MAX],bi[MAX],T,p,q,m,n,cas=1;
int main()
{
    freopen("/home/user/桌面/in","r",stdin);
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&m,&p,&q);
        scanf("%*d");
        for(int i=1;i<=p;i++)
        {
            scanf("%d",&a[i]);
            idx[a[i]]=i;//当a[i]范围很大时可用hash代替
        }
        n=0;
        scanf("%*d");
        for(int i=0;i<q;i++)
        {
            scanf("%d",&b);
            if(a[idx[b]]) bi[++n]=idx[b];
        }
        len[1]=bi[1];
        int l=1;
        for(int i=1;i<=n;i++)//求LIS
        {
            if(bi[i]>len[l]) len[++l]=bi[i];
            else *lower_bound(len+1,len+l+1,bi[i])=bi[i];
        }
        printf("Case %d: %d\n",cas++,l+1);
    }
    //printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
    return 0;
}
时间: 2024-11-07 05:55:58

UVA - 10635 最长公共子序列的相关文章

UVA 10635 Prince and Princess 最长公共子序列(nlongn)

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19051 题目意思是给你两串数字(计为 a,b 数组),让你求他们的最长公共子序列.数字长度是 n * n (n <= 250)所以 O(n^2) 肯定过不了了.所以想要找 O(nlongn)的. 因为题目给出了数字在 1 ~ (n^2)内并且数字不会重复.因此,对于a数组中的每个数字,如果我们都知道他在b数组中出先得位置的话(位置计为 c 数组),我们只需要在c数组里面求一

UVA 10100- Longest Match(dp之最长公共子序列)

题目地址:UVA 10100 题意:求两组字符串中最大的按顺序出现的相同单词数目. 思路:将字串中的连续的字母认作一个单词,依次计算出两个字符串中的单词,其中第1个字符串的单词序列为t1.word[1]-..t1.word[n],第2个字符串的单词序列为t2.word[1]-..t2.word[m].然后将每个单词当成一个字符,使用LCS算法计算出两个字符串的最长公共子序列,该序列的长度就是最长匹配. #include <stdio.h> #include <math.h> #in

uva 10405 Longest Common Subsequence (最长公共子序列)

uva 10405 Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, print the length of the longest common subsequence of both sequences. For example, the longest common subsequence of the following two sequences: abcdgh a

uva 10066 The Twin Towers (最长公共子序列)

uva 10066 The Twin Towers 题目大意:最长公共子序列. 解题思路:最长公共子序列. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std; int a[105], b[105], dp[105][105]; int main() { int n, m, Case = 1; while (scanf(

uva 11151 Longest Palindrome (最长公共子序列)

uva 11151 Longest Palindrome A palindrome is a string that reads the same from the left as it does from the right. For example, I, GAG and MADAM are palindromes, but ADAM is not. Here, we consider also the empty string as a palindrome. From any non-p

uva 10192 Vacation(最长公共子序列)

uva 10192 Vacation The Problem You are planning to take some rest and to go out on vacation, but you really don't know which cities you should visit. So, you ask your parents for help. Your mother says "My son, you MUST visit Paris, Madrid, Lisboa an

UVA 111 History Grading (最长公共子序列)

History Grading Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Background Many problems in Computer Science involve maximizing some measure according to constraints. Consider a history exam in which students are asked to put s

UVA 10723--Cyborg Genes+最长公共子序列变形

题目链接:点击进入 首先对于长度最短的情况是很容易确定的,只需要用两个字符串的长度和减去他们的最长公共子序列长度.然后比较麻烦的就是合乎要求的字符串的个数,其实我们也可以用类似于最长公共子序列的dp来求. 设dp[i][j]表示str1的前i个字符和str2的前j个字符所得到的满足要求的字符串,则如果str[i]==str[j],则dp[i][j]+=dp[i-1][j-1]; 否则就要根据i,j这两个位置上的最长公共子序列长度进行讨论,具体见代码. 代码如下: #include<iostrea

UVa 10066 Twin Towers (DP 最长公共子序列)

题意  求两串数字最长公共子序列的长度 裸的lcs没啥说的 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=105; int a[maxn],b[maxn],d[maxn][maxn],na,nb; void lcs() { memset(d,0,sizeof(d)); for(int i=1; i<=na; ++i) for(in