-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bacon Eggs and Spam.cpp
89 lines (79 loc) · 1.96 KB
/
Bacon Eggs and Spam.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <set>
#include <algorithm>
#include <cstring>
using namespace std;
void parseMenu(set<string>& menu, set<string>& nameSet, string order);
void displayMenuName(vector<string> menu, vector<string> name, vector<string> orderInfo);
//void displayData(vector<string>& menu); for debugging purpose
int main()
{
int customer;
cin >> customer;
cin.ignore();
while(customer != 0)
{
vector<string> orderInfo;
set<string> menuSet;
set<string> nameSet;
vector<string> menuVec;
vector<string> nameVec;
for(int index = 0; index < customer; index++)
{
string order;
getline(cin, order);
parseMenu(menuSet, nameSet, order);
orderInfo.push_back(order);
}
copy(menuSet.begin(), menuSet.end(), back_inserter(menuVec)); //convert set into vector for sorting
copy(nameSet.begin(), nameSet.end(), back_inserter(nameVec));
sort(menuVec.begin(), menuVec.end());
sort(nameVec.begin(), nameVec.end());
sort(orderInfo.begin(), orderInfo.end());
displayMenuName(menuVec, nameVec, orderInfo);
cin >> customer;
cin.ignore();
cout << endl;
}
}
void displayMenuName(vector<string> menu, vector<string> name, vector<string> orderInfo)
{
for(vector<string>::iterator it=menu.begin(); it != menu.end(); it++)
{
string menuName = *it;
cout << menuName << " ";
for(int i = 0; i < name.size(); i++)
{
if(orderInfo[i].find(menuName) != string::npos)
{
cout << name[i] << " ";
}
}
cout << endl;
}
}
void parseMenu(set<string>& menu, set<string>& nameSet, string order)
{
stringstream ss(order); //split the order input based on space
string name;
string menuName;
ss >> name;
nameSet.insert(name);
while(ss >> menuName)
{
if(menu.find(menuName) == menu.end()){
menu.insert(menuName);
}
}
}
/*
void displayData(vector<string>& menu)
{
cout << "*****" << endl;
for(vector<string>::iterator it=menu.begin(); it != menu.end(); ++it)
cout << *it << endl;
}
*/