牛客:HJ25 数据分类处理[华为机考][哈希][字符串]

学习要点

  1. 写代码时梳理好思路
  2. 写上注释

题目链接

数据分类处理_牛客题霸_牛客网

题目描述

解法:哈希+set

cpp 复制代码
#include <bits/stdc++.h>
#include <complex>
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;

int main() {
    // 先获得第一行
    int I_count;
    cin >> I_count;
    vector<string> v_str(I_count);
    for (int i = 0; i < I_count; i++) {
        cin >> v_str[i];
    }
    // 再获得第二行
    int R_count;
    cin >> R_count;
    vector<int> v_int(R_count);
    for (int i = 0; i < R_count; i++) {
        cin >> v_int[i];
    }

    // cout << endl;
    // for (auto& i : v_str) {
    //     cout << i << ' ';
    // }
    // cout << endl;
    // for (auto& i : v_int) {
    //     cout << i << ' ';
    // }
    // cout << endl;


    // 先处理R
    set<int> s_int(v_int.begin(), v_int.end());
    // cout << endl;
    // for (auto& i : s_int) {
    //     cout << i << ' ';
    // }
    // cout << endl;
    // 挂哈希桶
    unordered_map<int, vector<string>> index_map;
    for (auto& r_int : s_int) {
        for (int i = 0; i < I_count; i++) {
            int pos = v_str[i].find(to_string(r_int));
            if (pos != string::npos) {
                index_map[r_int].push_back(to_string(i));
            }
        }
    }
    // cout << endl;
    // for (auto& i : index_map) {
    //     cout << i.first << ' ';
    //     for(auto&j:i.second)
    //     {
    //         cout << j << " ";
    //     }
    //     cout << endl;
    // }
    // cout << endl;
    // 构建输出
    int real_R_count = index_map.size();
    // cout << real_R_count << endl;
    int O_count = 0;
    for (auto& i : index_map) {
        O_count += i.second.size() * 2;
    }
    // cout << O_count << endl;
    O_count += real_R_count * 2;
    // cout << O_count << endl;

    vector<string> O_str;
    O_str.push_back(to_string(O_count)); // 投入总数
    for (auto& r_int : s_int) {
        // 只处理有效R
        if (index_map[r_int].size() != 0) {
            // 投入单规则
            O_str.push_back(to_string(r_int));
            // 投入单规则总数
            O_str.push_back(to_string(index_map[r_int].size()));
            // 投入后续数组
            for (int k = 0; k < index_map[r_int].size(); k++) {
                // 投入下标
                string t1;
                t1 += index_map[r_int][k];
                O_str.push_back(t1);
                // 投入元素
                O_str.push_back(v_str[stoi(index_map[r_int][k])]);
            }
        }
    }

    for (auto& i : O_str) {
        cout << i << ' ';
    }
}
相关推荐
Black蜡笔小新5 小时前
自动化AI算法训练服务器DLTM助力医学影像分析进入AI智能分析新时代
人工智能·算法·自动化
手写码匠5 小时前
深入解析大模型架构之争:全能通用模型 vs 领域专精模型
人工智能·深度学习·算法·aigc
浅念-6 小时前
LeetCode 回溯算法题——综合练习
数据结构·c++·算法·leetcode·职场和发展·深度优先·dfs
列星随旋7 小时前
线段树和树状数组的学习
学习·算法
全糖可乐气泡水8 小时前
Codex适配国产信创环境安装部署与技术适配全解析
开发语言·git·python·算法·百度
h_a_o777oah9 小时前
状态机+划分型 DP :深度解析K-划分问题下 DP 状态的转移逻辑(洛谷P2679 P2331 附C++代码)
c++·算法·动态规划·acm·状态机dp·划分型dp·滚动数组优化
05候补工程师9 小时前
从算法理想向工程现实的跨越:SLAM 核心架构、思维误区与 Nav2 实战避坑指南
人工智能·算法·安全·架构·机器人
手写码匠10 小时前
Android 17 适配实战指南:新特性解读、隐私变更与迁移全攻略
人工智能·深度学习·算法·aigc
珊瑚里的鱼10 小时前
leetcode42雨水
算法·leetcode