ghit42796
12/20/2016 - 1:10 AM

Uva111.java

import java.util.*;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		int num = Integer.parseInt(scanner.nextLine());
		StringTokenizer StringAnswer = new StringTokenizer(scanner.nextLine()," ");
		boolean start = false;
		int answer[] = new int[num+1];
		while(scanner.hasNext()){
			int input[] = new int[num+1];
			StringTokenizer StringInput = new StringTokenizer(scanner.nextLine()," ");
			
			if(!start){
				for(int i=1;i<=num;i++){
					answer[i] = Integer.parseInt(StringAnswer.nextToken());
					input[i] = Integer.parseInt(StringInput.nextToken());
				}
				int temp[] = new int[num+1];
				for(int i=1;i<=num;i++){
					temp[answer[i]] = i;
				}
				for(int i=1;i<=num;i++){
					answer[i] = temp[i];
				}
				start = true;
			}else{
				for(int i=1;i<=num;i++){
					input[i] = Integer.parseInt(StringInput.nextToken());
				}
			}
			
			int temp[] = new int[num+1];
			for(int i=1;i<=num;i++){
				temp[input[i]] = i;
			}
			for(int i=1;i<=num;i++){
				input[i] = temp[i];
			}
			
			int LCS[][] = new int[num+1][num+1];
			for(int i=1;i<=num;i++){
				for(int j=1;j<=num;j++){
					if(answer[i] == input[j]){
						LCS[i][j] = LCS[i-1][j-1] + 1;
					}else{
						LCS[i][j] = Math.max(LCS[i-1][j], LCS[i][j-1]);
					}
				}
			}
			
			System.out.println(LCS[num][num]);
		}
	}

}