牛客: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 << ' ';
    }
}
相关推荐
gihigo19988 小时前
快速傅里叶变换(FFT)的应用
算法
做怪小疯子8 小时前
LeetCode 热题 100——二叉树——对称二叉树 & 二叉树的直径
算法·leetcode·职场和发展
Pluchon8 小时前
硅基计划4.0 算法 递归&回溯
算法·leetcode·决策树·深度优先·剪枝·广度优先·宽度优先
念安jy8 小时前
SDUT数据结构与算法pta--排序
算法
大江东去浪淘尽千古风流人物8 小时前
【MSCKF】零空间 UpdaterHelper::nullspace_project_inplace 的实现细节,MSCKF边缘化含义
算法·性能优化·vr·dsp开发·mr
AndrewHZ8 小时前
【图像处理基石】图像处理中的色彩经典算法原理与实战解析
图像处理·算法·计算机视觉·白平衡·色彩科学·经典算法·k means
Dev7z8 小时前
基于Matlab多算法的图像增强与客观质量评价系统
人工智能·算法·matlab
xcLeigh8 小时前
【新】Rust入门:基础语法应用
开发语言·算法·rust
小年糕是糕手9 小时前
【C++同步练习】类和对象(一)
java·开发语言·javascript·数据结构·c++·算法·排序算法
小O的算法实验室9 小时前
2025年IJPR SCI2区,基于混合邻域结构的高效稳定智能调度算法用于柔性作业车间调度,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进