Prince and Princess

时间:2020-04-09 12:37:11   收藏:0   阅读:87

In an n x n chessboard, Prince and Princess plays a game. The squares in the chessboard are numbered 1, 2, 3 ... n*n, as shown below:
Prince stands in square 1, make p jumps and finally reach square n*n. He enters a square at most once. So if we use xp to denote the p-th square he enters, then x1, x2, ... xp+1 are all different. Note that x1 = 1 and xp+1 = n*n. Princess does the similar thing - stands in square 1, make q jumps and finally reach square n*n. We use y1, y2 , ... yq+1 to denote the sequence, and all q+1 numbers are different.
Figure 2 belows show a 3x3 square, a possible route for Prince and a different route for Princess.
The Prince moves along the sequence: 1 --> 7 --> 5 --> 4 --> 8 --> 3 --> 9 (Black arrows), while the Princess moves along this sequence: 1 --> 4 --> 3 --> 5 --> 6 --> 2 --> 8 --> 9 (White arrow).
The King -- their father, has just come. "Why move separately? You are brother and sister!" said the King, "Ignore some jumps and make sure that you‘re always together."
For example, if the Prince ignores his 2nd, 3rd, 6th jump, he‘ll follow the route: 1 --> 4 --> 8 --> 9. If the Princess ignores her 3rd, 4th, 5th, 6th jump, she‘ll follow the same route: 1 --> 4 --> 8 --> 9, (The common route is shown in figure 3) thus satisfies the King, shown above. The King wants to know the longest route they can move together, could you tell him?
Input
The first line of the input contains a single integer t(1 <= t <= 10), the number of test cases followed. For each case, the first line contains three integers n, p, q(2 <= n <= 250, 1 <= p, q < n*n). The second line contains p+1 different integers in the range [1..n*n], the sequence of the Prince. The third line contains q+1 different integers in the range [1..n*n], the sequence of the Princess.

Output
For each test case, print the case number and the length of longest route. Look at the output for sample input for details.


Sample Input     
                     
1


3 6 7


1 7 5 4 8 3 9


1 4 3 5 6 2 8 9

 

Output for Sample Input

Case 1: 4
(Google翻译)

  在n x n棋盘上,王子和公主玩游戏。棋盘上的方块编号为1、2、3 ... n * n,如下所示:
王子站在方格1中,使p跳跃,最后到达方格n * n。他最多只能进入一个广场。因此,如果我们使用xp表示他输入的第p个平方,则x1,x2,... xp + 1都不同。注意,x1 = 1,xp + 1 = n * n。公主做类似的事情-站在方格1中,使q跳,最后到达方格n * n。我们使用y1,y2,... yq + 1表示序列,并且所有q + 1数均不同。
下面的图2显示了一个3x3的正方形,这是Prince的可能路线,而Princess的路线则不同。
王子按照以下顺序移动:1-> 7-> 5-> 4-> 8-> 3-> 9(黑色箭头),而公主按照以下顺序移动:1-> 4 -> 3-> 5-> 6-> 2-> 8-> 9(白色箭头)。
国王-他们的父亲刚来。 “为什么要分开走?你是兄弟姐妹!”国王说:“忽略一些跳跃,确保你们一直在一起。”
例如,如果王子忽略了他的第二,第三,第六跳,他将遵循以下路线:1-> 4-> 8->9。如果公主忽略了她的第三,第四,第五,第六跳,她将遵循相同的路线:1-> 4-> 8-> 9(常见路线如图3所示),因此满足了国王(如上所示)。国王想知道他们可以一起走的最长路线,你能告诉他吗?
输入值
  输入的第一行包含一个整数t(1 <= t <= 10),后面是测试用例的数量。对于每种情况,第一行包含三个整数n,p,q(2 <= n <= 250,1 <= p,q <n * n)。第二行包含p + 1个不同的整数,范围为[1..n * n],即Prince的序列。第三行包含q + 1个不同的整数,范围为[1..n * n](公主的序列)。

输出量
  对于每个测试案例,请打印案例编号和最长路径的长度。查看输出以获取样本输入以获取详细信息。


输入

1

3 6 7

1 7 5 4 8 3 9

1 4 3 5 6 2 8 9

输出

Case1:4

 

****根据题意我们可以概括为:两个没有重复元素的数组a,b(序列),求最长公共子序列LCS;

 

思路:

  我们可以借助数组做一个“映射”,也就是记录元素的位置;

  建立f1[ ]数组,将a序列“映射”到f1[ ]数组里,方法:f1[ 序列元素 ] = 序列元素编号;(这应该蛮好理解的)

  我们代入样例:

    f1[1] = 1;f1[7] = 2;f1[5] = 3;f1[4] = 4;f1[8] = 5;.............

  然后用这个“映射”映射出b的另一个数组(我们就叫b2吧),然后就是我们熟悉的求最长公共子序列;

(映射b2,我觉得我说的都有点懵,那就看一下代码,代码比我说的清楚)

  

 

#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>


using namespace std;
const int inf=0X3f3f3f3f;
const int N=100000;
int n,a[N],b[N],dp[N];
int A,B;
int f1[N],b2[N];
int maxn;
int Case=1;
int t;


int main(){
	scanf("%d",&t);
	
	while(t--){
		scanf("%d%d%d",&n,&A,&B);
		A++;
		B++;
		for(int i=1;i<=A;i++){
			scanf("%d",&a[i]);
			f1[a[i]]=i;
		}
		for(int i=1;i<=B;i++){
			scanf("%d",&b[i]);
			b2[i]=f1[b[i]];
		}
		for(int i=1;i<=B;i++){
			dp[i]=1;		
		}
		for(int i=1;i<=B;i++){
			maxn=0;			
			for(int j=1;j<i;j++){
				if(b2[j]<b2[i])maxn = max(maxn,dp[j]);			
			}
			dp[i]+=maxn;		
		}
		int ans = 0;
		for(int i=1;i<=B;i++){
			ans = max(ans,dp[i]);
		}
		printf("Case %d: %d\n",Case++,ans);
	}
	return 0;
}

  

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!