https://www.hackerrank.com/challenges/linkedin-practice-dictionaries-and-maps/problem
C++ code that passes all test cases
#include <iostream>#include <map>
using namespace std;
int main()
{
map<string,long>pb;
int n;
string name;
cin>>n;
for(int i=0;i<n;i++)
{
long num;
cin>>name>>num;
pb[name]=num;
}
while(cin>>name)
{
if(pb[name])
cout<<name<<"="<<pb[name]<<endl;
else
cout<<"Not found"<<endl;
}
}
C Code that passes only 2 test cases and timed out for other test cases:
#include <stdio.h>#include <stdlib.h>
#include <string.h>
struct node
{
int data;
char name[1000];
struct node *next;
};
int main()
{
int n;
scanf("%d",&n);
struct node *head[26]={NULL},*c;
for(int i=0;i<n;i++)
{
struct node * newnode=(struct node *)malloc(sizeof(struct node));
int val;
char temp[1000];
scanf("%s%d",temp,&val);
newnode->data=val; strcpy(newnode->name,temp);
newnode->next = NULL;
if(head[temp[0]-'a'] == NULL)
head[temp[0]-'a'] = newnode;
else
{
for(c=head[temp[0]-'a'];c->next != NULL;c=c->next);
c->next=newnode;
}
}
for(int i=0;i<n;i++)
{
char temp[1000];int flag=1;
scanf("%s",temp);
if(head[temp[0]-'a'] == NULL)
printf("Not found\n");
else
{
for(c=head[temp[0]-'a'];c!=NULL;c=c->next)
{
if(strcmp(temp,c->name) == 0)
{
printf("%s=%d\n",c->name,c->data);
flag=0;break;
}
}
if(flag)
printf("Not found\n");
}
}
return 0;
}
i also tried this program using c but i also have the same timeout error you mentioned . Is this possible to pass all the test case using c program ?.thank you .
ReplyDeletesolution in c
Delete#include
#include
#include
struct phonebook_entry {
char *name, *phone;
};
int compare_strings(const void *lhs, const void *rhs) {
return strcmp(*((const char **) lhs), *((const char **) rhs));
}
int main() {
// Caveat: no input validation,
// This program relies on a POSIX-2013 extension ('m') for
// allocating memory in scanf (see http://pubs.opengroup.org/
// onlinepubs/9699919799.2013edition/functions/scanf.html).
int n;
if (scanf("%d", &n) != 1) {
fprintf(stderr, "could not read 'n'\n");
exit(1);
}
// read the names and phone numbers
struct phonebook_entry entries[n];
for (int i = 0; i < n; ++i)
if (scanf("%ms %ms", &entries[i].name, &entries[i].phone) != 2) {
fprintf(stderr, "could not read phonebook entry #%d\n", i);
exit(1);
}
// sort for binary search
qsort(entries, n, sizeof(*entries), compare_strings);
// read names from stdin and look them up
char *name;
while (scanf("%ms", &name) == 1) {
const struct phonebook_entry *found = bsearch(
&name, entries, n, sizeof(*entries), compare_strings);
if (found)
printf("%s=%s\n", found->name, found->phone);
else
printf("Not found\n");
free(name);
}
// free all allocated memory
for (int i = 0; i < n; ++i) {
free(entries[i].phone);
free(entries[i].name);
}
}
its run
DeleteIs this possible to pass all the test case using c program ?.send me plz.....
ReplyDeleteu can solve it qsort(),bsearch() inbuilt functions in C and of course using strcmp. Try it
Deletesolution in c
Delete#include
#include
#include
struct phonebook_entry {
char *name, *phone;
};
int compare_strings(const void *lhs, const void *rhs) {
return strcmp(*((const char **) lhs), *((const char **) rhs));
}
int main() {
// Caveat: no input validation,
// This program relies on a POSIX-2013 extension ('m') for
// allocating memory in scanf (see http://pubs.opengroup.org/
// onlinepubs/9699919799.2013edition/functions/scanf.html).
int n;
if (scanf("%d", &n) != 1) {
fprintf(stderr, "could not read 'n'\n");
exit(1);
}
// read the names and phone numbers
struct phonebook_entry entries[n];
for (int i = 0; i < n; ++i)
if (scanf("%ms %ms", &entries[i].name, &entries[i].phone) != 2) {
fprintf(stderr, "could not read phonebook entry #%d\n", i);
exit(1);
}
// sort for binary search
qsort(entries, n, sizeof(*entries), compare_strings);
// read names from stdin and look them up
char *name;
while (scanf("%ms", &name) == 1) {
const struct phonebook_entry *found = bsearch(
&name, entries, n, sizeof(*entries), compare_strings);
if (found)
printf("%s=%s\n", found->name, found->phone);
else
printf("Not found\n");
free(name);
}
// free all allocated memory
for (int i = 0; i < n; ++i) {
free(entries[i].phone);
free(entries[i].name);
}
}