Pong loves to be picky about food and indulges in high-calorie meals, which leads to him gaining weight without growing taller. In daily life, we need various nutrients from food to maintain good health, so when choosing food, we should consider its nutritional value and benefits.
We need to create a filter for categorizing various foods for 小胖 to see which foods he can choose from to eat, to supplement his nutrition balance.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The first line contains a positive integer N (1 <= N <= 50), which represents the number of existing foods. The next N lines each contain two strings, F and S, separated by a space. The string F represents the name of the food (food names are unique), and the string S represents the type of food. The last line (line N + 1) contains a string C, which represents the food category to search for. (The strings F, S, and C do not contain spaces, and the number of characters in each string does not exceed 15.) | For each test case, output the names of the foods belonging to the searched category in dictionary order (from small to large). If there are no foods belonging to the searched category, output "No". |
2 apple fruit milk drink grains | No |
5 pork meat tea drink fish meat rice grains beef meat meat | beef fish pork |
Thought Process
Declare a Map<string, vector>, and when you receive F and S, use Map[S].push_back(F).
If Map[C].size() is 0, it means there are no foods of type C, so output "No". Otherwise, if there are foods of type C, first sort Map[C], then iterate over Map[C] using a for loop, and output each string in Map[C].
Sample Code-ZeroJudge E839: Food Sorting
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
map<string, vector<string>>MAP;
for (int i = 0; i<N; i++)
{
string a, b;
cin >> a >> b;
MAP[b].push_back(a);
}
string target;
cin >> target;
if (MAP[target].size() == 0) cout << "No\n";
else
{
vector<string>ans = MAP[target];
sort(ans.begin(), ans.end());
for (int i = 0; i<ans.size(); i++)
{
cout << ans[i] << "\n";
}
}
}
//ZeroJudge E839
//Dr. SeanXD