[Programming problem] Feature Extraction

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/m1f2c3/article/details/98995820

Links: https://www.nowcoder.com/questionTerminal/5afcf93c419a4aa793e9b325d01957e2?orderByHotValue=1&page=1&onlyReference=false&toCommentId=3445888
Source: Cattle-off network

[Programming questions] feature extraction
heat index: 944 Time limit: 1 second to space constraints: 32768K
algorithm knowledge of video to explain
Xiao Ming is an algorithm engineer, but also a shovel feces officer. One day, he whim, I wanted to tap some of the movement information from the video kitty cat's. In order to extract the motion information he needed "feature cats" are extracted from each frame of the video. Cat is a feature of a two-dimensional vector <x, y>. If x_1 = x_2 and y_1 = y_2, maybe it is the same feature.
Therefore, if a consistent feature of Kitten, Kitten can be considered in motion. That is, if the characteristic <a, b> appears in the frame in duration, it will constitute a feature movement. For example, wherein <a, b> 2/3/4/7/8 first frame appears, then the characteristic features forming the two motion 2-3-4 and 7-8.
Now, given the characteristics of each frame, the number of features may be different. Xiao Ming expect to find the longest feature movement.

Input Description:
The first line contains a positive integer N, the number of test cases are represented.

The first line of each test case contains a positive integer M, the representative video frames.

The next M rows, each row represents one. Wherein the first number is the number of the characteristics of the frame, the next number is the value of the characteristic; such as sample input in the third row, represents the frame has two features cat, <1,1> and <2,2>
the total number of input features with the embodiment and <100,000

N satisfies 1≤N≤100000, M number that satisfies the characteristics 1≤M≤10000, satisfying a ≤ 10000.
Characteristic values are non-negative integers.

Output Description:
As a test case for each line, length of the output characteristic of the motion
Example 1
Input
1
. 8
2 1 1 2 2
2 1 1 1. 4
2 1 1 2 2
2 2 2. 4 1
0
0
1 1 1
1 1 1
Output
3
described
wherein <1,1> in the consecutive three consecutive frames, a large number of consecutive compared to other features, the output 3

Note:
If no motion characteristic length greater than 2, the process returns 1

I did not expect to use the HashMap so bad, there is not even any number of issues beyond memory limit and the like.

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        for(int i = 0; i < N; ++i){
            HashMap<String, Integer> mem = new HashMap<>();
            HashMap<String, Integer> temp_mem = new HashMap<>();
            int M = sc.nextInt();
            int max = 1;
            for(int j = 0; j < M; ++j){
                temp_mem.clear();
                int n = sc.nextInt();
                for(int k = 0; k < n; ++k){
                    int x = sc.nextInt();
                    int y = sc.nextInt();
                    String key = String.valueOf(x) + " " + String.valueOf(y);
                    temp_mem.put(key, mem.getOrDefault(key, 0) + 1);
                    max = Math.max(temp_mem.get(key), max);
                }
                mem.clear();
                mem.putAll(temp_mem);
            }
            if(max <= 1){
                System.out.println(1);
            }else{
                System.out.println(max);
            }
        }

    }
}

Guess you like

Origin blog.csdn.net/m1f2c3/article/details/98995820