STL练习题:Delivery
来源:互联网 发布:天庭淘宝城宋小刀txt 编辑:程序博客网 时间:2024/05/17 02:48
Description
快递公司在运送货物的时候,会通过机器扫描得到货物信息。
但由于网络问题或重复扫描等问题,会产生冗余信息。
现在需要根据接收到的信息,进行整理并按时间排序,去除掉多余信息。
信息方式:
YYYY/MM/DD-HH:mm:ss|快递信息
要求保留某“快递信息”最早记录。
按时间顺序进行排序。
月:1-12
日:1-30
时:0-23
分、秒:0-59
输出格式
YYYY/MM/DD-HH:mm:ss : 快递信息
此题考查是查重,排序。难点在于这里需要将每个string输入拆分成两个substr分别操作。
比如输入是
2016/01/04-12:12:12|get
2016/01/04-12:23:42|get
要对get查重,而且要选择前面时间部分最早的一次记录。
这里可以用到string的find_first_of函数——str.find_first_of(“abc”)是指在str这个字符串中找”abc”,其返回的值是abc这3个字符中任何一个首次在str中出现的位置。
注意find_first_of函数和find函数的区别,如果是str.find(“abc”)的话,则必须找到完全匹配abc的才算查找成功,而find_first_of只需要abc中任何一个出现即可。
而从一个string中取出substr可以利用string的substr函数——str.substr(int pos, int count);第一个参数的意思是从某个位置pos开始取,第二个参数的意思是substr的字符个数为count个。
关于自动排序,排重等功能,可以利用set这个容器。
排重又可以利用map容器中key这个值的唯一性。
下面是解决代码:
//Delivery.hpp#include "vector"#include "string"#include "map"#include "set"using namespace std;vector<string> getInfoSort(vector<string> bf) { std::map<string, string> M; set<string> m; for (vector<string>::iterator it = bf.begin(); it != bf.end(); ++it) { string temp = *it; int off = temp.find_first_of("|"); int count = temp.size() - off; string key = temp.substr(off + 1, count); string n_key = temp.substr(0, off); M.insert(make_pair(key, n_key)); if (n_key.compare(M[key]) < 0) M[key] = n_key; } for (map<string,string>::iterator it = M.begin(); it != M.end(); ++it) { m.insert(it->second); } vector<string> afterSort; for (set<string>::iterator it = m.begin(); it != m.end(); ++it) { for (map<string,string>::iterator iter = M.begin(); iter != M.end(); ++iter) { if (iter->second == *it) { string t = iter->second; t += " : "; t += iter->first; afterSort.push_back(t); } } } return afterSort;}
//client.cpp#include <iostream>#include <vector>#include <string>#include "Delivery.hpp" using std::string; int main() { std::vector<string> info; int n = 0; std::cin >> n; for (int i = 0; i < n; i++) { std::string temp; std::cin >> temp; info.push_back(temp); } std::vector<string> afterSort = getInfoSort(info); for (std::vector<string>::iterator it = afterSort.begin(); it != afterSort.end(); ++it) { std::cout << *it << std::endl; } return 0;}
0 0
- STL练习题:Delivery
- STL练习题
- Delivery
- STL容器之练习题1
- 练习题 No.14 STL中的最大堆
- 尝试用STL完成Thinking in c++课后练习题
- UVa-156-Ananagrams STL map容器的使用练习题
- Delivery Completed
- Continuous Delivery
- Delivery SQL
- Content Delivery
- 练习题
- 练习题
- 练习题
- 练习题
- 练习题
- 练习题
- 练习题
- pom.xml报错:web.xml is missing and <failOnMissingWebXml> is set to true
- java String字符串的==解析
- JQuery选择器
- 2016年校赛 A题—地宫寻宝 建立三位数组的DP问题
- IEEE 802.15.4协议完整中文版 - 4.4 架构
- STL练习题:Delivery
- 关于使用imageloader遇到的问题记录
- HDU-2003
- centos7上搭建hadoop(基础篇)
- RadioButton
- win8.1开机出错pniopcac.exe报错win8.1系统安装STEP7 Micro/Win V4.0 SP9
- Universal-Image-Loader,android-Volley,Picasso、Fresco和Glide五大Android开源组件加载网络图片比较
- swift学习之路(十)字典
- Android开发--常用的传感器总结