特征提取-map

小明是一名算法工程师,同时也是一名铲屎官。某天,他突发奇想,想从猫咪的视频里挖掘一些猫咪的运动信息。为了提取运动信息,他需要从视频的每一帧提取“猫咪特征”。一个猫咪特征是一个两维的vector。如果x_1=x_2 and y_1=y_2,那么这俩是同一个特征。

       因此,如果喵咪特征连续一致,可以认为喵咪在运动。也就是说,如果特征在持续帧里出现,那么它将构成特征运动。比如,特征在第2/3/4/7/8帧出现,那么该特征将形成两个特征运动2-3-4 和7-8。

现在,给定每一帧的特征,特征的数量可能不一样。小明期望能找到最长的特征运动。

 

输入描述:

第一行包含一个正整数N,代表测试用例的个数。

每个测试用例的第一行包含一个正整数M,代表视频的帧数。

接下来的M行,每行代表一帧。其中,第一个数字是该帧的特征个数,接下来的数字是在特征的取值;比如样例输入第三行里,2代表该帧有两个猫咪特征,<1,1>和<2,2>
所有用例的输入特征总数和<100000

N满足1≤N≤100000,M满足1≤M≤10000,一帧的特征个数满足 ≤ 10000。
特征取值均为非负整数。

 

输入例子1:

1
8
2 1 1 2 2
2 1 1 1 4
2 1 1 2 2
2 2 2 1 4
0
0
1 1 1
1 1 1

 

输出例子1:

3

 



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
	
		int n = sc.nextInt();
		
		sc.nextLine();
        
        // 每个测试用例
		for (int i = 0; i < n; i++) {
			calcEachTestExample(sc);
		}
	}

	private static void calcEachTestExample(Scanner sc) {
		
		// 帧数
		int m = sc.nextInt();
		
		sc.nextLine();
		
		int maxCount = 0;
		ArrayList> rowList = new ArrayList>();
		for (int i = 0; i < m; i++) {
			
			int rowNum = sc.nextInt();
			
			HashMap map = new HashMap();
			for (int j = 0; j < 2 * rowNum - 1; j = j + 2) {
				Features fd = new Features(sc.nextInt(), sc.nextInt());
				
				int fdcount = 1;
				// 最大特征数是上层最大加1
				if (rowList.size() > 0 && rowList.get(i - 1).containsKey(fd)) {
					fdcount = rowList.get(i - 1).get(fd) + 1;
				}
				
				map.put(fd, fdcount);
				
				maxCount = Math.max(maxCount, fdcount);
			}
			
			rowList.add(map);
			sc.nextLine();
		}
		
		System.out.println(maxCount);
	}
	
	public static class Features {
		int x;
		int y;
		
		public Features(int a, int b) {
			x = a;
			y = b;
		}
		
		@Override
		public boolean equals(Object o) {
			if (o instanceof Features) {
				return x == ((Features)o).x && y == ((Features)o).y;
			}
			return false;
		}
		
		@Override
		public int hashCode() {
			return Integer.hashCode(x & y);
		}
	}
}

 

你可能感兴趣的:(算法题,Java)