假设我们有一个数组订单,它代表顾客在餐厅里完成的订单。因此,orders [i] = [cust_namei,table_numi,food_itemi],其中cust_namei是客户名称,table_numi是客户表号,而food_itemi是商品客户订单。
我们必须退还餐厅的“展示桌”。这里的“显示表”是一个表,其行条目表示每个表订购的每种食品有多少。第一列为表格编号,其余各列按字母顺序对应于每个食品。第一行应为标题,其第一列为“表格”,然后是食品名称。
因此,如果输入像orders = [["Amal","3","Paratha"],["Bimal","10","Biryni"],["Amal","3","Fried Chicken"],["Raktim","5","Water"],["Raktim","5","Paratha"],["Deepak","3","Paratha"]],则输出将是[["Table","Biryni","Fried Chicken","Paratha","Water"],["3","0","1","2","0"],["5","0","0","1","1"],["10","1","0","0","0"]]]]
让我们看下面的实现以更好地理解-
#include <bits/stdc++.h> using namespace std; void print_vector(vector<vector<string> > v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << "["; for(int j = 0; j <v[i].size(); j++){ cout << v[i][j] << ", "; } cout << "],"; } cout << "]"<<endl; } typedef long long int lli; class Solution { public: vector <string> split(string& s, char delimiter){ vector <string> tokens; string token; istringstream tokenStream(s); while(getline(tokenStream, token, delimiter)){ tokens.push_back(token); } return tokens; } static bool cmp(vector <string>& a, vector <string>& b){ lli an = stol(a[0]); lli bn = stol(b[0]); return an < bn; } vector<vector<string>> displayTable(vector<vector<string>>& o) { map <string, map < string, int> >m; set <string> names; set <string> t; for(auto &it : o){ t.insert(it[1]); bool ok = true; if(!ok){ vector <string> v = split(it[2], ' '); for(auto& x : v){ m[it[1]][x]++; names.insert(x); } } else{ m[it[1]][it[2]]++; names.insert(it[2]); } } vector < vector <string> > ret; vector <string> temp(names.begin(), names.end()); temp.insert(temp.begin(), "Table"); ret.push_back(temp); for(auto& it : t){ vector <string> te; te.push_back(it); for(auto& x : names){ te.push_back(to_string(m[it][x])); } ret.push_back(te); } sort(ret.begin() + 1, ret.end(), cmp); return ret; } }; main(){ Solution ob; vector<vector<string>> v = {{"Amal","3","Paratha"},{"Bimal","10","Biryni"},{"Amal","3","Fried Chicken"},{"Raktim","5","Water"},{"Raktim","5","Paratha"},{"Deepak"," 3","Paratha"}}; print_vector(ob.displayTable(v)); }
{{"Amal","3","Paratha"},{"Bimal","10","Biryni"},{"Amal","3","Fried Chicken"},{"Raktim","5","Water"},{"Raktim","5","Paratha"},{"Deepak"," 3","Paratha"}}
输出结果
[[Table, Biryni, Fried Chicken, Paratha, Water, ],[3, 0, 1, 2, 0, ],[5, 0, 0, 1, 1, ],[10, 1, 0, 0, 0, ],]