牛客: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 << ' ';
    }
}
相关推荐
唐梓航-求职中6 分钟前
编程大师-技术-算法-leetcode-355. 设计推特
算法·leetcode·面试
●VON11 分钟前
HarmonyOS应用开发实战(基础篇)Day01-《ArkTS基本知识》
学习·华为·harmonyos·鸿蒙·von
少许极端13 分钟前
算法奇妙屋(二十八)-递归、回溯与剪枝的综合问题 1
java·算法·深度优先·剪枝·回溯·递归
仰泳的熊猫14 分钟前
题目1453:蓝桥杯历届试题-翻硬币
数据结构·c++·算法·蓝桥杯
唐梓航-求职中14 分钟前
技术-算法-leetcode-1606. 找到处理最多请求的服务器(易懂版)
服务器·算法·leetcode
啊阿狸不会拉杆15 分钟前
《机器学习导论》第 10 章-线性判别式
人工智能·python·算法·机器学习·numpy·lda·线性判别式
BlackWolfSky16 分钟前
鸿蒙高级课程笔记2—应用性能优化
笔记·华为·harmonyos
会叫的恐龙17 分钟前
C++ 核心知识点汇总(第11日)(排序算法)
c++·算法·排序算法
twilight_46917 分钟前
机器学习与模式识别——线性回归算法
算法·机器学习·线性回归
玄同76524 分钟前
Python Random 模块深度解析:从基础 API 到 AI / 大模型工程化实践
人工智能·笔记·python·学习·算法·语言模型·llm