博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[重新解答]阿里笔试:去重和排序,重新输出Markdown格式
阅读量:4092 次
发布时间:2019-05-25

本文共 1743 字,大约阅读时间需要 5 分钟。

在网上偶然看到一道题目,发现博主的答案有问题,所以重新解答一下题目

原题链接【
在这里插入图片描述


#include 
#include
#include
#include
#include
#include
#include
using namespace std;static vector
> file;void preReadFile(){ ifstream fin ( "case.txt" ); string line; string line_sign; int line_data; stringstream ss; while ( ss.clear(), ss.str ( "" ), getline ( fin, line ) ) { ss << line; ss >> line_sign; ss >> line_data; file.push_back ( make_pair ( line_sign, line_data ) ); // cout << ss.str() <<"|"<< line_sign << "|" << line_data << endl; }}typedef int CATEGORY;typedef set
DATA;map
LEVEL_1, LEVEL_2;void parseFile(){ CATEGORY pre_lv1_category = 0; CATEGORY pre_lv2_category = 0; for ( int i = 0; i < file.size(); ++i ) { auto &cur_category = file[i].first; auto &cur_data = file[i].second; // cout << cur_category << "|" << cur_data << endl; if ( cur_category == "#" ) { pre_lv1_category = cur_data; LEVEL_1.insert (make_pair (pre_lv1_category, set
())); } else if ( cur_category == "##" ) { pre_lv2_category = cur_data; LEVEL_1[pre_lv1_category].insert (cur_data); LEVEL_2.insert (make_pair (pre_lv2_category, set
())); } else if ( cur_category == "*" ) { LEVEL_2[pre_lv2_category].insert (cur_data); } else { cout << "Parse Error" << endl; } }}int main ( int argc, const char **argv ){ preReadFile(); parseFile(); for ( auto it1 = LEVEL_1.begin(); it1!=LEVEL_1.end(); ++it1 ) { cout << "# " <
first << endl; for (auto it2 = it1->second.begin(); it2!=it1->second.end(); ++it2) { cout << "## " <<*it2 << endl; for (auto it3 = LEVEL_2[*it2].begin(); it3!=LEVEL_2[*it2].end(); ++it3) { cout << "* " <<*it3 << endl; } } } return 0;}

运行结果:

在这里插入图片描述


测试文件(case.txt):# 1## 33* 506* 809# 2## 4600* 132* 2345## 5960* 34# 1## 33* 506* 506

转载地址:http://wrcii.baihongyu.com/

你可能感兴趣的文章
思岚A1的SDK其实很好读懂,每个函数清晰明了,可以直接调用
查看>>
串级 PID 为什么外环输出是内环的期望?(和我之前对串级PID的总结一样)
查看>>
我刚刚才完全清楚GPS模块的那根杆子是怎么固定安装好的
查看>>
去github里面找找也没有别人无人机+SLAM的工程
查看>>
现在明白为什么无名博客里好几篇文章在讲传感器的滞后
查看>>
Pixhawk解锁常见错误
查看>>
ROS是不是可以理解成一个虚拟机,就是操作系统之上的操作系统
查看>>
用STL algorithm轻松解决几道算法面试题
查看>>
ACfly之所以不怕炸机因为它觉得某个传感器数据不安全就立马不用了
查看>>
我发觉,不管是弄ROS OPENCV T265二次开发 SDK开发 caffe PX4 都是用的C++
查看>>
ROS的安装(包含文字和视频教程,我的ROS安装教程以这篇为准)
查看>>
原来我之前一直用的APM固件....现在很多东西明白了。
查看>>
realsense-ros里里程计相关代码
查看>>
似乎写个ROS功能包并不难,你会订阅话题发布话题,加点逻辑处理,就可以写一些基础的ROS功能包了。
查看>>
if __name__ == ‘__main__‘:就是Python里的main函数,脚本从这里开始执行,如果没有main函数则从上到下顺序执行。
查看>>
PX4官方用户和开发手册的首页面是会给你选择英文和中文的
查看>>
《跟工程师学嵌入式开发》这本书最后的终极项目我反而觉得有说头
查看>>
博士的申请考核制
查看>>
MAVLink学习之路05_MAVLink应用编程接口分析(也有讲STM32下的收发函数)
查看>>
找到了中文版的mavlink手册
查看>>