Prince and Princess UVA
来源:互联网 发布:天威网络工程公司 编辑:程序博客网 时间:2024/06/06 08:56
题目传送门
题意:求两个序列的最长公共子序列。
思路:如果用最朴素的算法,显然会超时,所以用LIS的方式对LCS进行一个优化使O(N^2)转化为O(N*logN)的时间复杂度的算法(这个实现要求两个序列当中重复的元素比较少,这个题目两个序列没有重复的元素,所以没问题)。
#include <algorithm>#include <cmath>#include <cstdio>#include <cstring>#include <iostream>#include <list>#include <map>#include <queue>#include <set>#include <stack>#include <string>#include <vector>#define MAXN 100100#define MAXE 5#define INF 1000000000#define MOD 10007#define LL long long#define ULL unsigned long long#define pi 3.14159using namespace std;int a[MAXN];int b[MAXN];int change[MAXN];int dp[MAXN];int arr[MAXN];int main() { std::ios::sync_with_stdio(false); int n, m, k, T; cin >> T; for (int kase = 1; kase <= T; ++kase) { cin >> k >> n >> m; n++, m++; memset(change, -1, sizeof(change)); int cnt = 0; for (int i = 1; i <= n; ++i) { cin >> a[i]; change[a[i]] = i; } for (int i = 1; i <= m; ++i) { cin >> b[i]; if (change[b[i]] != -1) { dp[cnt++] = change[b[i]]; } } fill(arr, arr + MAXN, INF); for (int i = 0; i < cnt; ++i) { *lower_bound(arr, arr + cnt, dp[i]) = dp[i]; } cout << "Case " << kase << ": " << lower_bound(arr, arr + cnt, INF) - arr << endl; } return 0;}/* 1 3 6 7 1 7 5 4 8 3 9 1 4 3 5 6 2 8 9 */
阅读全文
0 0
- Prince and Princess UVA
- uva 10635 Prince and Princess
- UVA 10635 Prince and Princess
- Uva-10635-Prince and Princess
- UVA 10635 Prince and Princess
- uva 10635 - Prince and Princess
- UVA 10635 Prince and Princess
- UVA 10635 Prince and Princess
- uva 10635 - Prince and Princess
- UVA - 10635 Prince and Princess
- uva 10635 - Prince and Princess
- UVA 10635 - Prince and Princess
- UVA 10635 Prince and Princess
- UVA-10635 Prince and Princess
- uva 10635 - Prince and Princess
- uva 10635 Prince and Princess
- UVA - 10635 Prince and Princess
- UVA - 10635 Prince and Princess
- Linux查找含有某字符串的所有文件
- uboot移植-内存分布
- Java 将获取的阿拉伯数字转换成对应的中文
- Android学习随笔(3)------活动的生命周期
- 超值商品找券平台
- Prince and Princess UVA
- HttpsURLConnection实现SSL的GET/POST请求
- 同一服务器部署多个tomcat时的端口号修改详情
- 微信小程序 Jgank 开发(一)
- 并行流与顺序流性能对比试验
- css3
- < 笔记 > DOM
- zookeeper 学习总结
- Android Framework--转屏原理