#include <iostream> #include <algorithm> using namespace std; const int maxn = 250 * 250; int main(){ char T; cin >> T; for (char k = 0; k < T;k++) { int n, p, q; cin >> n >> p >> q; int num[maxn]; memset(num, 0, sizeof(num)); int temp; for (int i = 0; i < p; i++){ cin >> temp; num[temp] = i; } int S[maxn]; int size = 0; for (int i = 0; i < q; i++) { cin >> temp; if (num[temp]){ S[size++] = num[temp]; } } //降低复杂度, 由于这些数字并不重复, 所以可以将他们映射到1-n //这样, 公共子序列问题就转化为 公共递增子序列问题 //下面是LIS算法 int stack[maxn]; for (int i = 0; i < maxn; i++) stack[i] = maxn; int ans = 1; for (int i = 0; i < maxn; i++){ int count = lower_bound(stack + 1, stack + n + 1, S[i]) - stack; if (count>ans) ans = count; stack[count] = S[i]; } cout << "Case " << k <<": "<< ans<<endl; } }
时间: 2024-10-07 18:29:24