UVA - 10635 Prince and Princess

来源:互联网 发布:高仿椰子 知乎 编辑:程序博客网 时间:2024/06/08 18:58

题目大意:给出n, m, k,求两个长度分别为m + 1 和 k + 1且由1~n * n组成的序列的最长公共子序列长的。


解题思路:按一般的o(n^2)的算法超时了,所以要LCS装换成LIS的算法o(nlogn)。算法仅仅是将其中的一个序列重新标号1~m,然后按最长公共子序列的方法去做。

#include <cstdio>#include <algorithm>#include <cstring>using namespace std;int main() {int T, cas = 0, pos[62600], DP[62600] = {0};scanf("%d", &T);while (T--) {int n, p, q, x, cnt = 0;scanf("%d%d%d", &n, &p, &q);memset(pos, 0, sizeof(pos));for (int i = 0; i <= p; i++) {scanf("%d", &x);pos[x] = i + 1;}for (int i = 0; i <= q; i++) {scanf("%d", &x);if (pos[x] > DP[cnt])DP[++cnt] = pos[x];elseDP[lower_bound(DP + 1, DP + 1 + cnt, pos[x]) - DP] = pos[x];}printf("Case %d: %d\n", ++cas, cnt);}return 0;}


0 0
原创粉丝点击