随笔 - 531  文章 - 0  评论 - 3  阅读 - 10215 

两个数组a,b,首元素都为1,a[i]  b[i]<=K^2

求最长公共子序列的长度

 

暴力LCS会T,但注意 两个序列的元素都 <=K^2

对b[i] 找到 a[j] 配对(不存在为0) ,构造一个新序列,变为求LIS


复制代码
#include <iostream>
 #include <cstring>
 #include <algorithm> 
 using namespace std;
  const int N=26e4;
  int cmp(int x,int y){
      return x<y;
  }
  int n,p,q,b[N],c[N],d[N],len;
  
  int main(){
  //    freopen("in","r",stdin);
      int i,l,x,tes,cas;
      cin>>tes;
      
      for(cas=1;cas<=tes;cas++){
      cin>>n>>p>>q;
      memset(c,0,sizeof c);
      for(i=1;i<=p+1;i++) cin>>x,c[x]=i;
      
      len=1;
      for(i=1;i<=q+1;i++){
           cin>>x; if(c[x]) b[++len]=c[x];
       }
       l=1; d[1]=b[1];
       
       for(i=2;i<=len;i++){
        if(b[i]>d[l]) d[++l]=b[i];
        else *lower_bound(d+1,d+1+l,b[i],cmp)=b[i];
    }
    printf("Case %d: %d\n",cas,l-1);
    }
    
  }
  
  
  
复制代码

 

posted on   towboat  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示