首页 > 试题广场 >

特征提取

[编程题]特征提取
  • 热度指数:12674 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
       小明是一名算法工程师,同时也是一名铲屎官。某天,他突发奇想,想从猫咪的视频里挖掘一些猫咪的运动信息。为了提取运动信息,他需要从视频的每一帧提取“猫咪特征”。一个猫咪特征是一个两维的vector<x, y>。如果x_1=x_2 and y_1=y_2,那么这俩是同一个特征。
       因此,如果喵咪特征连续一致,可以认为喵咪在运动。也就是说,如果特征<a, b>在持续帧里出现,那么它将构成特征运动。比如,特征<a, b>在第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

输出

3

说明

特征<1,1>在连续的帧中连续出现3次,相比其他特征连续出现的次数大,所以输出3

备注:
如没有长度大于2的特征运动,返回1
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int N = in.nextInt(); //测试用例次数
        for (int i = 0; i < N; i++) {
            int M = in.nextInt(); //帧数
            //由于每一帧的特征数量不一定,因此用一个集合来进行存储
            List<List<Integer>> list = new ArrayList<>();
            //用来定义dp数组的列
            int max = 0;

            for (int j = 0; j < M; j++) {
                int n1 = in.nextInt();
                List<Integer> temp = new ArrayList<>();
                temp.add(n1);
                max = Math.max(max, n1);
                for (int k = 0; k < n1 * 2; k++) {
                    int num = in.nextInt();
                    temp.add(num);
                }
                list.add(temp);
            }

            int[][] dp = new int[M][max]; //dp[i][j]表示到第i帧j个特征的最大特征长度
            for (int j = 0; j < M; j++) {
                Arrays.fill(dp[j],1); //默认本身都是1,但是对于特征数为0的情况在后面遍历的时候进行处理
            }
           
            int result = 1;

            for (int j = 1; j < M; j++) {
                //从第一个开始遍历
                if (list.get(j).get(0) == 0) {
                    //如果当前的特帧数为0,那么这一行的所有dp都为0;
                    for(int k = 0; k < max; k++){
                        dp[j][k] = 0;
                    }
                    continue;
                }

                //帧数不为0
                int count2 = 0;

                for (int k = 1; k < list.get(j).size(); k += 2) {
                    //c表示上一行的数据的下标,用来进行比对的
                    //count1表示上一行dp的列数,也就是第二个参数
                    int c = 1;
                    int count1 = 0;
                    //用一个while循环来对当前行的第k和k+1个数与上一行的所有数据进行比对
                    while (c < list.get(j - 1).size()) {
                        //如果当前的[k,k+1]等于[c,c+1],那么这一行这个位置上的dp就等于上一行这个位置上的dp+1
                        //然后找最大值
                        if (list.get(j).get(k) == list.get(j - 1).get(c) &&
                                list.get(j).get(k + 1) == list.get(j - 1).get(c + 1)) {
                            dp[j][count2] = dp[j - 1][count1] + 1;
                            result = Math.max(result, dp[j][count2]);
                            //System.out.println(result + " j= " + j + " k= " + k);
                        }
                        //如果当前的[k,k+1]不等于[c,c+1],那么就将c+2,并且将上一行的dp的位置也要往前移一个,也就是count1+1;
                        c += 2;
                        count1++;
                    }
                    //到这一步,表示这一行的k和k+1已经对比完成,然后要对比下一对数据,那么这一行的dp也要对应+1
                    count2++;
                }
            }
            System.out.println(result);

        }
    }
}
发表于 2023-10-04 21:20:58 回复(0)
import java.util.HashMap;
import java.util.Scanner;

//把vector拼成一个字符串,当作哈希的key存放,对应的value存放该特征的连续长度count和上一次出现的视频帧lastFrame
public class Main {

    static class Stat{
        int lastFrame = 0;
        int count = 0;
        public Stat(int lastFrame, int count){
            this.lastFrame = lastFrame;
            this.count = count;
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int cntUsage = in.nextInt();
        int cntFrame = 0;
        int cntStat = 0;
        int res = 1;
        HashMap<String,Stat> map = new HashMap<>();
        // 注意 hasNext 和 hasNextLine 的区别
        int iUsage = 0;
        while (iUsage < cntUsage) {
            int iFrame = 0;
            cntFrame = in.nextInt();
            while (iFrame < cntFrame) {
                int iStat = 0;
                cntStat = in.nextInt();
                while (iStat < cntStat) {
                    int a = in.nextInt();
                    int b = in.nextInt();
                    String str = a + "," + b;
                    if (!map.containsKey(str)){
                        map.put(str,new Stat(iFrame,1));
                    }else{
                        Stat stat = map.get(str);
                        if (iFrame - stat.lastFrame == 1){ //连续特征累加count
                            stat.count++;
                            if (stat.count > res){ //找最大特征长度
                                res = stat.count;
                            }
                        }else{
                            stat.count = 1;
                        }
                        stat.lastFrame = iFrame;
                    }
                    iStat++;
                }
                iFrame++;
            }
            iUsage++;
        }
        System.out.println(res);
    }
}

发表于 2023-08-09 00:20:25 回复(0)

自定义一个Point用来存放点,然后将point存放在map中,每次都进行更新。每一帧都新建一个map,若上一帧中存在该点,则将该点对应的值+1,并存放在map中,判断是否为新的最大。若上一帧不存在该点,则存放入map中,value为1

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        HashMap<Point,Integer> points = new HashMap<Point,Integer>();
        for(int i = 0; i < n; i ++){
            int m = sc.nextInt();
            int res = 0;
            for(int j = 0; j < m; j ++){
                int p = sc.nextInt();
                HashMap<Point,Integer> tempPoints = new HashMap<Point,Integer>();
                for (int k = 0; k < p; k ++){
                    int x = sc.nextInt();
                    int y = sc.nextInt();
                    Point temp = new Point(x,y);
                    Integer num = points.get(temp);
                    if (num == null){
                        num = new Integer(1);
                    } else{
                        num += 1;
                    }
                    res = num > res ? num : res;
                    tempPoints.put(temp,num); 
                }
                points.clear();
                points = tempPoints;
            }
            System.out.print(res);
        }
   }
}

class Point{
    int x;
    int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public Point() {
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Point point = (Point) o;
        return x == point.x &&
                y == point.y;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y);
    }
}
发表于 2021-02-10 16:49:45 回复(0)
其实还算简单
对每个用例建一个map,键为坐标字符串比如,2 1 1 2 2,可以得到两个键(1,1)和(2,2),我是以逗号区分开,对应的value为一个List,存放该键出现在哪些帧。
最后得到了这些信息,就遍历这个map,在内遍历list看连续帧数就行
不过费解的是,官方为什么说没有大于2的就输出1,当时就在想那2就直接输出1呗
果然,2就是2 ,真坑,表述不清,还靠提交试错

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for(int i = 0;i < n;i++){
            int pic = in.nextInt();
            in.nextLine();
            Map<String,List<Integer>> map = new HashMap<>();
            int res = 1;
            for(int j = 0;j < pic;j++){
                String s = in.nextLine();
                String[] str = s.split(" ");
                int num = Integer.parseInt(str[0]);
                StringBuilder sb = new StringBuilder();
                for(int k = 1;k < str.length;k += 2){
                    sb.append(str[k]).append(",").append(str[k+1]);
                    String tmp = sb.toString();
                    if(!map.containsKey(tmp)){
                        List<Integer> list = new ArrayList<>();
                        list.add(j);
                        map.put(tmp,list);
                    }else{
                        map.get(tmp).add(j);
                    }
                    sb.delete(0,sb.length());
                }
            }
            for(String s:map.keySet()){
                List<Integer> tmp = map.get(s);
                int count = 1;
                for(int q = 1;q < tmp.size();q++){
                    if(tmp.get(q) == tmp.get(q-1) + 1){
                        count++;
                    }else{
                        count = 1;
                    }
                    res = Math.max(res,count);
                }
            }
            System.out.println(res);
        }
    }
}


发表于 2020-06-07 20:57:25 回复(0)
记录一下
import java.util.HashMap;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        while (N-- > 0){
            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){
                    String key = sc.nextInt() + " " + sc.nextInt();
                    temp_mem.put(key, mem.getOrDefault(key, 0) + 1);
                    max = Math.max(temp_mem.get(key), max);
                }
                mem.clear();
                mem.putAll(temp_mem);
            }
            System.out.println(max);
        }
    }
}

发表于 2020-05-06 16:19:14 回复(0)
Java解法
import java.util.HashMap;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int size = scanner.nextInt();
        int num = scanner.nextInt();
        HashMap<String, Integer> last = new HashMap<>();
        HashMap<String, Integer> current = new HashMap<>();
        int max=0;
        for (int i = 0; i < num; i++) {
            int pairNum = scanner.nextInt();
            for (int j = 0; j < pairNum; j++) {
                String key= scanner.nextInt()+"_"+scanner.nextInt();
                if (last.get(key)==null){
                    current.put(key,1);
                }else {
                    current.put(key,last.get(key)+1);
                }
            }
            for (Integer value : current.values()) {
                max=Math.max(value,max);
            }
            last=current;
            current=new HashMap<>();
        }
        System.out.println(max);
    }
}


发表于 2020-03-01 23:26:25 回复(0)
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int ans = 0;
		Map<Reference, Integer> map1 = new HashMap<Reference, Integer>();
		Map<Reference, Integer> map2 = new HashMap<Reference, Integer>();
		for(int i=0;i<n;i++) {
			int m = scanner.nextInt();
			map1.clear();
			for(int j=0;j<m;j++) {
				 int p = scanner.nextInt();
				 if(p==0) {
					 map1.clear();
					 continue;
				 }
				 for(int k=0;k<p;k++) {
					 int x = scanner.nextInt();
					 int y = scanner.nextInt();
					 Reference reference = new Reference(x, y);
					 if(map1.containsKey(reference)) {
						 map2.put(reference, map1.get(reference)+1);
					 }else {
						 map2.put(reference, 1);
					 }
					 ans = Math.max(ans, map2.get(reference));
				 }
				 map1.clear();
				 map1.putAll(map2);
				 map2.clear();
			}
		}
		System.out.println(ans);
	}
	
	
	
	
}
class Reference{
	int x;
	int y;
	public Reference(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}
	@Override
	public int hashCode() {
		return  x + y + x*y;
	}
	@Override
	public boolean equals(Object obj) {
		Reference reference = (Reference)obj;
		return x==reference.x && y==reference.y;
	}
	
}

发表于 2019-10-30 16:26:50 回复(1)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int N = input.nextInt();
        int[] M = new int[N];
        int[][] num = new int[N][];
        int[][][][] feats= new int[N][][][];
        for(int i = 0; i < N; i++) {
            M[i] = input.nextInt();
            num[i] = new int[M[i]];
            feats[i] = new int[M[i]][][]; 
        	for(int j = 0; j < M[i]; j++) {
        		num[i][j] = input.nextInt();
        		feats[i][j] = new int[num[i][j]][];
        		for(int k = 0; k < num[i][j]; k++) {
        			feats[i][j][k] = new int[2];
        			feats[i][j][k][0] = input.nextInt();
        			feats[i][j][k][1] = input.nextInt();
        		}
        	}
        }
        input.close();
        for(int i = 0; i < N; i++)
        	System.out.println(Solution(M[i], num[i], feats[i]));
    }
    public static int Solution(int M, int[] num, int[][][] feats) {
		int max = 1;
		if(M == 1)
			return max;
		for(int i = 0; i < M-1; i++) {
			for(int j = 0; j < num[i]; j ++) {
				if(feats[i][j] != null) {
					int sum = 1;
					for(int x = i + 1; x < M; x++) {
						for(int y = 0; y < num[x]; y++) {
							if(Equals(feats[x][y], feats[i][j])) {
								sum ++;
								feats[x][y] = null;
								break;
							}
						}if(sum != x-i+1 || x == M-1) {
							max = (max < sum) ? sum : max;
							break;
						}
					}
				}
			}
		}
    	return max;    	
    }
    public static boolean Equals(int[] a, int[] b){
        if(a == null || b == null)
            return false;
        if(a[0] == b[0] && a[1] == b[1])
           return true;
        else
            return false;
    }
}


编辑于 2019-09-06 12:13:25 回复(0)
从头到尾遍历
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for (int i=0;i<n;i++){
            int m = in.nextInt();
            Pair[][] pairs = new Pair[m][];
            for (int j=0;j<m;j++){
                int t = in.nextInt();
                pairs[j] = new Pair[t];
                for (int k=0;k<t;k++){
                    pairs[j][k] = new Pair(in.nextInt(),in.nextInt());
                }
            }
            int max = Integer.MIN_VALUE;
            for (int j=0;j<m;j++){          //遍历每一帧
                for (int k=0;k<pairs[j].length;k++){         //遍历每一特征
                    if (pairs[j][k] != null){         //如果特征不为null
                        int c = 1;          //特征初始次数为1
                        int temp = c;
                        for (int t=j+1;t<m;t++){         //遍历当前帧以下的所有帧
                            for (int s = 0;s<pairs[t].length;s++){      //遍历每一特征
                                if (pairs[t][s] != null && pairs[j][k].first == pairs[t][s].first
                                        && pairs[j][k].second == pairs[t][s].second){     //若特征不为null,且相等
                                    pairs[t][s] = null;         //将其标志为null,防止后序重复遍历
                                    c++;      //特帧数加1
                                }
                            }
                            if (temp == c){    //遍历一个帧后,特征数不变,说明其已不连续了,此时退出
                                break;
                            }else {
                                temp = c;
                            }
                        }//还有一种退出情况,即循环到尾部,自动退出了
                        if (c > max)
                            max = c;    //记录最大特帧数
                    }
                }
            }
            System.out.println(max);
        }
    }

    private static class Pair{
        int first;
        int second;
        Pair(int first,int second){
            this.first = first;
            this.second = second;
        }
    }
}


编辑于 2019-08-18 16:04:57 回复(0)