The problem:
The solution:
Objective
Today, we're learning about Key-Value pair mappings using a Map or Dictionary data structure. Check out theTutorial tab for learning materials and an instructional video!
Today, we're learning about Key-Value pair mappings using a Map or Dictionary data structure. Check out theTutorial tab for learning materials and an instructional video!
Task
Given names and phone numbers, assemble a phone book that maps friends' names to their respective phone numbers. You will then be given an unknown number of names to query your phone book for; for each queried, print the associated entry from your phone book (in the form ) or if there is no entry for .
Given names and phone numbers, assemble a phone book that maps friends' names to their respective phone numbers. You will then be given an unknown number of names to query your phone book for; for each queried, print the associated entry from your phone book (in the form ) or if there is no entry for .
Note: Your phone book should be a Dictionary/Map/HashMap data structure.
Input Format
The first line contains an integer, , denoting the number of entries in the phone book.
Each of the subsequent lines describes an entry in the form of space-separated values on a single line. The first value is a friend's , and the second value is an -digit .
Each of the subsequent lines describes an entry in the form of space-separated values on a single line. The first value is a friend's , and the second value is an -digit .
After the lines of phone book entries, there are an unknown number of lines of queries. Each line (query) contains a to look up, and you must continue reading lines until there is no more input.
Note: Names consist of lowercase English letters and are first names only.
Constraints
Output Format
On a new line for each query, print if the name has no corresponding entry in the phone book; otherwise, print the full and in the format .
Sample Input
3
sam 99912222
tom 11122222
harry 12299933
sam
edward
harry
Sample Output
sam=99912222
Not found
harry=12299933
Explanation
We add the subsequent (Key,Value) pairs to our map so it looks like this:
We then process each query and print if the queried Key is found in the map, or otherwise.
Query 0:
Sam is one of the keys in our dictionary, so we print .
Sam is one of the keys in our dictionary, so we print .
Query 1:
Edward is not one of the keys in our dictionary, so we print .
Edward is not one of the keys in our dictionary, so we print .
Query 2:
Harry is one of the keys in our dictionary, so we print .
Harry is one of the keys in our dictionary, so we print .
The solution:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n, number;
cin >> n;
string name1, name2;
map<string, int> list;
for(int i=0;i<n;i++){
cin>>name1;
cin>>number;
cin.ignore(); //needed to ignore the space after the number, and bring to newline
list[name1]=number;
}
while(getline(cin,name2)){
if(list.find(name2) !=list.end()){
cout<< name2 << "=" <<list[name2]<<endl;
}
else
cout<<"Not found"<<endl;
}
return 0;
}
No comments:
Post a Comment