Repository navigation
Expand file tree
/
Copy pathCode03_PermutationLCS.java
More file actions
92 lines (78 loc) · 2.07 KB
/
Copy pathCode03_PermutationLCS.java
File metadata and controls
92 lines (78 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package class087;
// 两个排列的最长公共子序列长度
// 给出由1~n这些数字组成的两个排列
// 求它们的最长公共子序列长度
// n <= 10^5
// 测试链接 : https://www.luogu.com.cn/problem/P1439
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
// 提交以下的code,提交时请把类名改成"Main",可以直接通过
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class Code03_PermutationLCS {
public static int MAXN = 100001;
public static int[] a = new int[MAXN];
public static int[] b = new int[MAXN];
public static int[] where = new int[MAXN];
public static int[] ends = new int[MAXN];
public static int n;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(br);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF) {
n = (int) in.nval;
for (int i = 0; i < n; i++) {
in.nextToken();
a[i] = (int) in.nval;
}
for (int i = 0; i < n; i++) {
in.nextToken();
b[i] = (int) in.nval;
}
out.println(compute());
}
out.flush();
out.close();
br.close();
}
public static int compute() {
for (int i = 0; i < n; i++) {
where[a[i]] = i;
}
for (int i = 0; i < n; i++) {
b[i] = where[b[i]];
}
return lis();
}
// 讲解072 - 最长递增子序列及其扩展
public static int lis() {
int len = 0;
for (int i = 0, find; i < n; i++) {
find = bs(len, b[i]);
if (find == -1) {
ends[len++] = b[i];
} else {
ends[find] = b[i];
}
}
return len;
}
public static int bs(int len, int num) {
int l = 0, r = len - 1, m, ans = -1;
while (l <= r) {
m = (l + r) / 2;
if (ends[m] >= num) {
ans = m;
r = m - 1;
} else {
l = m + 1;
}
}
return ans;
}
}