牛客: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 << ' ';
    }
}
相关推荐
vibecoding日记16 小时前
双非如何快速入职字节等大厂大模型?真实案例分析:推理优化和投机解码
算法·求职·大模型工程师
yszaygr213818 小时前
Verilog参数化游程编码RLE模块
算法
望易18 小时前
刚设计的大模型架构-双域耦合认知框架
算法·架构
复杂网络1 天前
多个 Claude Code 与多个 Codex 协同工作:设计与实现方案
算法
HjhIron2 天前
面试常客:字符串算法从入门到进阶
算法·面试
吴佳浩2 天前
DeepSeek DSpark:Confidence-Scheduled Speculative Decoding 技术解析
人工智能·算法·deepseek
触底反弹2 天前
🧠 搞懂 Token,才算真正入门大模型——从分词原理到 Embedding 语义实战
javascript·人工智能·算法
vivo互联网技术2 天前
ICLR 2026 | 基于后验采样的图像恢复方法LearnIR:人脸去阴影、去雾
人工智能·算法·aigc
浮生望2 天前
JS字符串与回文算法:从包装类到双指针的面试进阶之路
javascript·算法