牛客: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 << ' ';
    }
}
相关推荐
神龙斗士24018 分钟前
Java 数组的定义与使用
java·开发语言·数据结构·算法
Y.O.U..22 分钟前
力扣HOT100-跳跃游戏II
算法·leetcode
hn小菜鸡23 分钟前
LeetCode 3132.找出与数组相加的整数 II
算法·leetcode·职场和发展
微笑尅乐27 分钟前
数组模拟加法——力扣66.加一
算法·leetcode·职场和发展
_不会dp不改名_44 分钟前
leetcode_146 LRU缓存
算法·leetcode·缓存
帅帅爱数学2 小时前
DeepMimic论文详细解析:基于示例引导的深度强化学习实现物理仿真角色技能
算法·强化学习
我是华为OD~HR~栗栗呀2 小时前
测试转C++开发面经(华为OD)
java·c++·后端·python·华为od·华为·面试
IT成长日记2 小时前
【LVS入门宝典】LVS调度算法轮询(RR)深度解析:从原理到实战的公平调度之道
算法·lvs·rr·轮询调度算法
NAGNIP3 小时前
一文搞懂量化、剪枝和知识蒸馏都是什么?
算法
点云SLAM3 小时前
GTSAM 中自定义因子(Custom Factor)的详解和实战示例
算法·机器人·slam·后端优化·gtsam·gtsam自定义因子·因子图