c++ - Improving Time Hackerrank [terminated due to timeout] -
i've created code based of information given me below.
"the first line have integer n denoting number of entries in phone book. each entry consists of 2 lines: name(either first-name or first-name last-name) , corresponding phone number.
after these, there queries. each query contain name of friend. read queries until end-of-file."
also if name not found supposed print "not found"
the problem on hackerrank.com , cant submit because program "terminated due timeout"
can please me improve code , make run faster?! barely figured out how make run i'm out of ideas on how improve it.
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <string> #include <vector> using namespace std; class number{ public: string name; string lname; long number; }; class phonebook{ public: void findnumber(){ string tempname; while (cin >> tempname){ int nf=0; for(int i=0; < phonenumbers.size(); i++){ if (tempname == phonenumbers[i].name){ cout << phonenumbers[i].name << "=" << phonenumbers[i].number << endl; } else { nf +=1; } } if (nf == phonenumbers.size()){cout << "not found" << endl;} } } void addnumber(int entries){ for(int numbers = 0; numbers < entries; numbers++){ number newcontact; long num; cin >> newcontact.name; cin >> num; if (!cin){ cin.clear(); cin >> newcontact.lname >> newcontact.number; } else{ newcontact.number = num; } phonenumbers.push_back(newcontact); } } private: string pbname; vector<number> phonenumbers; }; int main(){ int entries; cin >> entries; phonebook myfriends; myfriends.addnumber(entries); myfriends.findnumber(); return 0; }
Comments
Post a Comment