题解 | 特征提取

特征提取

https://www.nowcoder.com/practice/5afcf93c419a4aa793e9b325d01957e2

解题思路

这是一个特征运动检测的问题。我们需要根据每一帧的特征信息,找出特征在连续帧中出现的最长次数。

关键点:

  1. 使用字典记录每个特征的连续出现次数。
  2. 在每一帧中更新特征的出现次数。
  3. 通过交换历史和当前特征的字典来管理连续出现的特征。

算法步骤:

  1. 读取测试用例的数量。
  2. 对于每个测试用例,读取帧数和每帧的特征信息。
  3. 使用两个字典分别记录当前帧和历史帧的特征出现次数。
  4. 在遍历每一帧时,更新特征的出现次数,并记录最大连续出现次数。
  5. 清空当前帧的特征信息,将其转为历史帧信息,准备下一帧的处理。

代码

#include <iostream>
#include <map>
#include <utility>

using namespace std;

int main() {
    int testCases, frames;
    cin >> testCases;

    while (testCases--) {
        cin >> frames;
        int featureCount, x, y;

        map<pair<int, int>, int> previous; // 存储历史帧数
        map<pair<int, int>, int> current;  // 存储当前帧数

        int maxLength = 0; // 记录最长特征运动的长度

        for (int i = 0; i < frames; i++) {
            cin >> featureCount;

            for (int j = 0; j < featureCount; j++) {
                cin >> x >> y;

                // 更新当前特征的出现次数
                if (previous.count({x, y})) {
                    current[{x, y}] = previous[{x, y}] + 1; // 增加出现次数
                } else {
                    current[{x, y}] = 1; // 新特征,初始化为1
                }

                // 更新最大连续出现次数
                if (current[{x, y}] > maxLength) {
                    maxLength = current[{x, y}];
                }
            }

            // 清空当前帧的历史信息
            previous.clear();
            previous.swap(current); // 将当前帧信息转为历史帧信息
        }

        cout << maxLength << endl; // 输出结果
    }

    return 0;
}
import java.util.*;

public class Main {
    // 自定义Pair类
    static class Pair<K, V> {
        K key;
        V value;

        Pair(K key, V value) {
            this.key = key;
            this.value = value;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof Pair)) return false;
            Pair<?, ?> pair = (Pair<?, ?>) o;
            return Objects.equals(key, pair.key) && Objects.equals(value, pair.value);
        }

        @Override
        public int hashCode() {
            return Objects.hash(key, value);
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int testCases = sc.nextInt();

        while (testCases-- > 0) {
            int frames = sc.nextInt();
            Map<Pair<Integer, Integer>, Integer> previous = new HashMap<>();
            Map<Pair<Integer, Integer>, Integer> current = new HashMap<>();
            int maxLength = 0;

            for (int i = 0; i < frames; i++) {
                int featureCount = sc.nextInt();
                for (int j = 0; j < featureCount; j++) {
                    int x = sc.nextInt();
                    int y = sc.nextInt();
                    Pair<Integer, Integer> feature = new Pair<>(x, y);

                    current.put(feature, previous.getOrDefault(feature, 0) + 1);
                    maxLength = Math.max(maxLength, current.get(feature));
                }
                previous.clear();
                previous.putAll(current);
                current.clear();
            }
            System.out.println(maxLength);
        }
        sc.close();
    }
}
class Pair:
    def __init__(self, key, value):
        self.key = key
        self.value = value
    
    def __eq__(self, other):
        if not isinstance(other, Pair):
            return False
        return self.key == other.key and self.value == other.value
    
    def __hash__(self):
        return hash((self.key, self.value))

def solve_feature_motion():
    test_cases = int(input())
    
    for _ in range(test_cases):
        frames = int(input())
        previous = {}  # 存储历史帧的特征计数
        current = {}   # 存储当前帧的特征计数
        max_length = 0
        
        for i in range(frames):
            frame_data = list(map(int, input().split()))
            feature_count = frame_data[0]
            
            # 处理当前帧的每个特征
            for j in range(feature_count):
                x = frame_data[2*j + 1]
                y = frame_data[2*j + 2]
                feature = Pair(x, y)
                
                # 更新特征的连续出现次数
                current[feature] = previous.get(feature, 0) + 1
                max_length = max(max_length, current[feature])
            
            # 更新历史帧信息
            previous.clear()
            previous.update(current)
            current.clear()
        
        print(max_length)

if __name__ == "__main__":
    solve_feature_motion()

算法及复杂度

  • 算法:模拟
  • 时间复杂度:,每个特征在每帧中最多出现一次
  • 空间复杂度:,用于存储特征的出现次数
全部评论

相关推荐

行云流水1971:这份实习简历的优化建议: 结构清晰化:拆分 “校园经历”“实习经历” 板块(当前内容混杂),按 “实习→校园→技能” 逻辑排版,求职意向明确为具体岗位(如 “市场 / 运营实习生”)。 经历具象化:现有描述偏流程,需补充 “动作 + 数据”,比如校园活动 “负责宣传” 可加 “运营公众号发布 5 篇推文,阅读量超 2000+,带动 300 + 人参与”;实习内容补充 “协助完成 XX 任务,效率提升 X%”。 岗位匹配度:锚定目标岗位能力,比如申请运营岗,突出 “内容编辑、活动执行” 相关动作;申请市场岗,强化 “资源对接、数据统计” 细节。 信息精简:删减冗余表述(如重复的 “负责”),用短句分点,比如 “策划校园招聘会:联系 10 + 企业,组织 200 + 学生参与,到场率达 85%”。 技能落地:将 “Office、PS” 绑定经历,比如 “用 Excel 整理活动数据,输出 3 份分析表;用 PS 设计 2 张活动海报”,避免技能单独罗列。 优化后需强化 “经历 - 能力 - 岗位需求” 的关联,让实习 / 校园经历的价值更直观。 若需要进一步优化服务,私信
实习,投递多份简历没人回...
点赞 评论 收藏
分享
Edgestr:没项目地址就干脆把那一栏删了呗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务