【UVA 10635】【LCS转化成LIS】 Prince and Princess
来源:互联网 发布:美国城建 知乎 编辑:程序博客网 时间:2024/03/29 21:43
思路:
这个题目显然用n^2的最长公共子序列的算法会超时,这题最长公共子序列可以转化成最长上升子序列的问题,然后用最长上升子序列的nlogn的算法求解。
但这个方法要求任意一个字符在两序列中出现的频率都要比较低,因为转化成最长上升子序列的算法的最坏复杂度是n^2log(n^2)。而这个题目恰好每个数字最多只在两序列中各出现一次,所以就可以达到nlogn的复杂度了。
代码:
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int maxn=251*251;const int inf=0x3f3f3f3f;int g[maxn],d[maxn],a[maxn],num[maxn];int n,ans;void LIS(){ for(int i=1; i<=n; i++)g[i]=inf; for(int i=0; i<n; i++){ int k=lower_bound(g+1, g+n+1, a[i])-g;//在g[1]到g[n]中找 d[i]=k; g[k]=a[i]; ans=max(ans, d[i]); }}int main(){ /* #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif*/ int t;scanf("%d",&t); int kase=1; while(t--){ int N,p,q,x; scanf("%d%d%d",&N,&p,&q); memset(num, 0, sizeof(num)); n=0;ans=0; for(int i=1; i<=p+1; i++){ scanf("%d",&x); num[x]=i;} for(int i=0; i<q+1; i++){ scanf("%d",&x); if(num[x])a[n++]=num[x];} LIS(); printf("Case %d: %d\n",kase++,ans); } return 0;}
0 0
- 【UVA 10635】【LCS转化成LIS】 Prince and Princess
- uva 10635 Prince and Princess (将LCS 转化为 LIS)
- UVA 10635 - Prince and Princess LCS转化为LIS
- UVA - 10635 - Prince and Princess (LCS转化为LIS)
- uva-10635 Prince and Princess LCS转化为LIS
- UVA 10635 Prince and Princess LCS转化为LIS *
- UVA 10635 Prince and Princess lcs--》lis
- dp(LCS转化成LIS)uva 10635 - Prince and Princess
- uva 10635 Prince and Princess(LCS问题转化成LIS问题O(nlogn))
- Uva - 10635 - Prince and Princess(LCS转LIS)
- UVA 10635 Prince and Princess (LCS优化转LIS)
- UVa 10635 Prince and Princess / LCS 转 LIS
- uva 10635Prince and Princess (LCS转LIS)
- UVA 10635 - Prince and Princess (LCS转成LIS DP)
- UVA - 10635 Prince and Princess LCS转LIS
- UVA 10635 - Prince and Princess ( LCS 转换为LIS )
- UVa 10635 - Prince and Princess(LCS转LIS)
- uva 10635-Prince and Princess 【LIS 求解 LCS】
- TIJ_16章 数组
- 微信小应用-基础组件
- FileWriter&FileReader
- PopupWindow背景变暗
- Android简单的菜单笔记
- 【UVA 10635】【LCS转化成LIS】 Prince and Princess
- 深入理解Java 注解原理
- 聊聊Socket、TCP/IP、HTTP、FTP及网络编程
- javaArray 学习笔记
- JZOJ4796 【GDOI2017模拟9.21】三色图 构造可行解
- Android之NetworkOnMainThreadException异常
- 【心情】2016-9-23
- c++中的左值与右值
- openjudge-noi-2.5-2971:抓住那头牛