450 - Little Black Book

All about problems in Volume 4. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

David Jones
New poster
Posts: 3
Joined: Fri Jun 30, 2006 5:49 am

Post by David Jones »

Beware the spacing!
title/first name may be an empty string

dust_cover
New poster
Posts: 23
Joined: Tue Sep 12, 2006 9:46 pm

Getiing RTE

Post by dust_cover »

Can someone tell me why am I getting repeatedly RTE (signal 11)???
I have used the limits discussed here....but still have no clue for solution.

Code: Select all

//little black book
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define MAX 20001
//#define M 65

struct lil{

	char dept[63]; 
	char title[28];
	char fname[28];
	char lname[28];
	char hadd[53];
	char hph[53];
	char wph[33];
	char cbox[33];  
	
}book[MAX];

int cmp(const void *a,const void *b)
{
   lil *p=(lil *)a;
   lil *q=(lil *)b;
   return strcmp( p->lname,q->lname );
} 

int main(void)
{
	int test, i, flag=1, flagi=0, flagn, m=0;
	char ch='\0';
	
	//freopen("450.txt","r", stdin);
	//freopen("0.out","w", stdout);
	scanf("%d", &test);
	ch=getc(stdin);
	
	while(1)
	{
		
		flagn=0;
		if(flag)gets(book[m].dept);
		
		//-----------------title
		if(flagi)i=1;
		else if(!flagi)i=0;
		while(ch!=',')
		{
			ch=getc(stdin);
			book[m].title[i++]= ch;
		}
		book[m].title[i-1]='\0';
		ch='\0';
		//--------------------first name
		
		i=0;
		while(ch!=',')
		{
			ch=getc(stdin);
			book[m].fname[i++]= ch;
		
		}
		book[m].fname[i-1]='\0';
		ch='\0';
		//------------------last name
		
		i=0;
		while(ch!=',')
		{
			ch=getc(stdin);
			book[m].lname[i++]= ch;
		
		}
		book[m].lname[i-1]='\0';
		ch='\0';
		//----------------home address
		
		i=0;
		while(ch!=',')
		{
			ch=getc(stdin);
			book[m].hadd[i++]= ch;
		
		}	
		book[m].hadd[i-1]='\0';
		ch='\0';
		
		//----------------home phone
		
		i=0;
		while(ch!=',')
		{
			ch=getc(stdin);
			book[m].hph[i++]= ch;
		
		}
		book[m].hph[i-1]='\0';
		ch='\0';
		//----------------work phone
		
		i=0;
		while(ch!=',')
		{
			ch=getc(stdin);
			book[m].wph[i++]= ch;
		
		}
		book[m].wph[i-1]='\0';
		ch='\0';
		//---------------campus box
		i=0;
		while(ch!='\n' || ch!=EOF)
		{
			ch=getc(stdin);
			if(ch=='\n')
			{
				flagn=1;
				break;
			}
			else if(ch==EOF)
				break;
			book[m].cbox[i++]= ch;
				
		}
		
		book[m].cbox[i]='\0';
		m++;
		
	
		if(ch==EOF)
			break;
		
		ch='\0';
		ch=getc(stdin);
	
		if(ch=='\n'&& flagn==1)
		{
			flag=1;
			flagi=0;
		}
		else
		{
			i=0;
			book[m].title[i]=ch;
			flagi=1;
			flag=0;
	
		}
	}
	qsort(book,m,sizeof(book[0]),cmp);
    
	for(i=0;i<m;i++)
	{
		printf("----------------------------------------\n");
		printf("%s %s %s\n", book[i].title, book[i].fname, book[i].lname);
		printf("%s\n", book[i].hadd);
		printf("Department: %s\n",book[i].dept);
		printf("Home Phone: %s\n", book[i].hph);
		printf("Work Phone: %s\n",book[i].wph);
		printf("Campus Box: %s\n", book[i].cbox);
					
	}

	return 0;
}
--Thnx in advance!
i wanna give it a try....

xtremedreamer
New poster
Posts: 5
Joined: Sun Dec 21, 2008 7:19 am

450!!! compilation error

Post by xtremedreamer »

I have used stl to solve 450. But getting compilation error
Any body plz help.
Here is my code:

Code: Select all

#include <stdio.h>
#include <iostream>
#include <vector>
#include <list>
#include <string>
using namespace std;

int main(){
	string dept, MemberInfo, temp, fin; 
	list<string>facultyMember;
	list<string>LastName;
	int numDept, countDept;
	
	//freopen("M:\\Documents and Settings\\prince\\Desktop\\450.txt","rt",stdin);
	while (true){
		getline(cin,temp,'\n');
		if (temp == "")
			break;
		numDept = atoi(temp.c_str());
		countDept = 0;		

		while (countDept < numDept){
			getline(cin, dept, '\n');
			while (( getline(cin, MemberInfo,'\n') && MemberInfo != "")){
				temp = MemberInfo;
				temp.erase(0,temp.find(",",0)+1);
				temp.erase(0,temp.find(",",0)+1);
				string temp1(temp.begin(),temp.find(",",0));
				LastName.push_back(temp1);
				LastName.sort();
				list<string>::iterator it = LastName.begin();
				list<string>::iterator itFM = facultyMember.begin();
				for (int i = 0; i < LastName.size(); i++){
					if (*it != temp1){
						it++;
						itFM++;
					}
					else
						break;
				}
				facultyMember.insert(itFM,dept+","+MemberInfo);
			}
			countDept++;
			
		}
		list<string>::iterator itFM = facultyMember.begin();
		int i = 0;
		while(i++ < facultyMember.size()){
			printf("----------------------------------------\n");
			string temp = *itFM;
			dept = string(temp.begin(),temp.find(",",0));
			temp.erase(0,temp.find(",",0)+1);
			for (int i = 0; i < 3; i++){
				string str(temp.begin(),temp.find(",",0));
				temp.erase(0,temp.find(",",0)+1);
				cout <<str <<" ";
			}
			string str(temp.begin(),temp.find(",",0));
			temp.erase(0,temp.find(",",0)+1);
			cout <<"\n" << str<<"\n";
			cout <<"Department: "<<dept<<"\n";
			cout <<"Home Phone: ";
			str = string(temp.begin(),temp.find(",",0));
			temp.erase(0,temp.find(",",0)+1);
			cout << str <<"\n";
			cout <<"Work Phone: ";
			str = string(temp.begin(),temp.find(",",0));
			temp.erase(0,temp.find(",",0)+1);
			cout << str <<"\n";
			cout <<"Campus Box: ";
			cout << temp <<"\n";
			
			itFM++;
		}
	}
return 0;
}
Thanks in advance

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: whatz wrong with 450 help please

Post by mf »

My g++ complains about this line (and all other similar lines in your code):
string temp1(temp.begin(),temp.find(",",0));
because it doesn't know a constructor of std::string whose parameters are std::string's iterator, and a size_t.
Did you mean to write temp1(temp.begin(), temp.begin() + temp.find(",",0))?

xtremedreamer
New poster
Posts: 5
Joined: Sun Dec 21, 2008 7:19 am

Re: whatz wrong with 450 help please

Post by xtremedreamer »

Thanks mf. but now i am getting time limit exceed. I can't figure out why i am getting time limit
exceed. Plz anybody provide necessary inputs for which my code gets stuck. Here is my code:

Code: Select all

#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <list>
#include <string>
using namespace std;

int main(){
	string dept, MemberInfo, temp, fin, str, temp1; 
	list<string>facultyMember;
	list<string>LastName;
	int i, numDept, countDept, start, pos;
	string::iterator it;
	char ch[1000];
	while (1 == scanf("%d",&numDept)){
		facultyMember.clear();
		LastName.clear();
		gets(ch);
		countDept = 0;
		while(countDept < numDept){
			gets(ch);
			dept = string(ch);
			if (dept == "")
				continue;
			while (gets(ch) != NULL){
				MemberInfo = string(ch);
				if (MemberInfo == "")
					break;
				temp = MemberInfo;
				temp.erase(0,temp.find(",",0)+1);
				temp.erase(0,temp.find(",",0)+1);
				temp1 = string(temp.begin(),temp.begin()+temp.find(',',0));
				LastName.push_back(temp1);
				LastName.sort();
				list<string>::iterator it = LastName.begin();
				list<string>::iterator itFM = facultyMember.begin();
				for (i = 0; i < LastName.size(); i++){
					if (*it != temp1){
						it++; itFM++;
					}
					else break;
				}
				facultyMember.insert(itFM,dept+","+MemberInfo);
			}
			countDept++;
		}
		list<string>::iterator itFM = facultyMember.begin();
		int i = 0;
		while(i++ < facultyMember.size()){
			printf("----------------------------------------\n");
			string temp = *itFM;
			dept = string(temp.begin(),temp.begin()+temp.find(",",0));
			temp.erase(0,temp.find(",",0)+1);
			for (int l = 0; l < 3; l++){
				str = string(temp.begin(),temp.begin()+temp.find(",",0));
				temp.erase(0,temp.find(",",0)+1);
				printf("%s ",str.c_str());
			}
			str = string(temp.begin(),temp.begin()+temp.find(",",0));
			temp.erase(0,temp.find(",",0)+1);
			printf("\n%s\n",str.c_str());
			printf("Department: %s\n",dept.c_str());
			printf("Home Phone: ");
			
			str = string(temp.begin(),temp.begin()+temp.find(",",0));
			temp.erase(0,temp.find(",",0)+1);
			printf("%s\n",str.c_str());
			printf("Work Phone: ");
			
			str = string(temp.begin(),temp.begin()+temp.find(",",0));
			temp.erase(0,temp.find(",",0)+1);
			printf("%s\n",str.c_str());
			printf("Campus Box: ");
			printf("%s\n",temp.c_str());
			itFM++;
		}
	}
return 0;
}

Moniqui
New poster
Posts: 1
Joined: Wed Jun 15, 2011 10:34 pm

I need help with this problem can anyone help me please 450

Post by Moniqui »

450-Little black book
i don't know why it is wrong i've got to mucho run time error
please can someone help me
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int xNroDep=in.nextInt();
int y=xNroDep;
int yx=0;
in.nextLine();

int fil =0;
int col=0;
int yet=0;
String []Ape=new String[900];
String dep="";

if(xNroDep>1 && xNroDep<13)
{String []apell23=new String[900];
int i=0;
while(yx<y)
{
String x=in.nextLine();
if(x.length()!=0)
{
if(x.length()<26)
yx++;

apell23=x;
i++;
}

}


while(in.hasNext())
{String x=in.nextLine();
apell23=x;
i++;


String cad="";
dep="";




yet=0;
String [][]mat=new String[90][900];

for(int j=0;j<=i;j++)
{ cad=apell23[j]+",";
if(cad.length()>25)
{ if(cad.length()<236)
{ String aux="";
for(int k=0;k<cad.length();k++)
{ if(cad.charAt(k)==',')
{ if(col==2)
{ mat[fil][col]=aux.trim();

Ape[yet]=aux;
yet ++;

}
if(col==4)
{
mat[fil][col]=dep;
col++;
mat[fil][col]=aux.trim();

}
if(col!=2 && col!=4)
{
mat[fil][col]=aux.trim();

}



aux="";
col++;


}
else
{ aux=aux+cad.substring(k,k+1);
}
}
fil++;
col=0;
}
}
else
{ if(cad!=null)
{ dep=cad.substring(0,cad.length()-1).trim();
}
}
}


Arrays.sort(Ape,0,yet);
col=0;

int j=0;
while(j<fil)
{
if(mat[col][2]==Ape[j])
{
imprimir(col,mat);
elimina(mat,fil,col);
col=0;

j++;
}
else
col++;
}

}
}



}


static void imprimir(int f,String mat[][])
{
System.out.printf("----------------------------------------\n");
System.out.printf("%s %s %s\n",mat[f][0],mat[f][1],mat[f][2]);

System.out.printf("%s\n",mat[f][3]);

System.out.printf("Department: %s\nHome Phone: %s\nWork Phone: %s\nCampus Box: %s\n",mat[f][4],mat[f][5],mat[f][6],mat[f][7]);

}
static void elimina(String mat[][],int n,int is)
{
for(int j=0;j<n ;j++)
{ if(j==is)
{
for(int l=0;l<8;l++)
mat[j][l]=mat[j+1][l];

n--;
}

}
}
}

uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: I need help with this problem can anyone help me please

Post by uDebug »

This is seemingly simple but very tricky / weird problem.

The only way to get an AC solution (that I know of) is

(1) Create a struct to store the data

(2) Push this struct into a vector

(3) Write a custom compare to sort the vector by last names


I tried several other ways that seemed equivalent but they ended up getting WA. The above method was the only one that worked.

Here's some other important information that I learned along the way

(a) There are no more than 3500 entries.

(b) Apparently, each entry is unique. So, no two people have the same last name.


If you're struggling with this problem, please don't hesitate to reach out for help by replying to this message.

Anyway, so, I don't have any good test input / output to share because the judge's input's plain strange. But here's one just to make sure that the basics of your code are correct.

Code: Select all

3
English Department
Dr.,Tom,Davis,Anystreet USA,+1-555-2832,+1-555-2423,823
Mrs.,Jessica,Lembeck,Center Street,+1-555-2543,+1-555-8584,928
Dr.,Bob,Davisa,Anywherestreet USA,545-2832,555-2222,911

Computer Science
Mr.,John,Euler,East Pleasure CA,+1-555-1432,+1-555-2343,126

Geology
Ms.,Aisha,Ang,West Pleasure TT,+868-525-1422,+868-166-2343,666
AC Output:

Code: Select all

----------------------------------------
Ms. Aisha Ang
West Pleasure TT
Department: Geology
Home Phone: +868-525-1422
Work Phone: +868-166-2343
Campus Box: 666
----------------------------------------
Dr. Tom Davis
Anystreet USA
Department: English Department
Home Phone: +1-555-2832
Work Phone: +1-555-2423
Campus Box: 823
----------------------------------------
Dr. Bob Davisa
Anywherestreet USA
Department: English Department
Home Phone: 545-2832
Work Phone: 555-2222
Campus Box: 911
----------------------------------------
Mr. John Euler
East Pleasure CA
Department: Computer Science
Home Phone: +1-555-1432
Work Phone: +1-555-2343
Campus Box: 126
----------------------------------------
Mrs. Jessica Lembeck
Center Street
Department: English Department
Home Phone: +1-555-2543
Work Phone: +1-555-8584
Campus Box: 928
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: I need help with this problem can anyone help me please

Post by brianfry713 »

There was probably a bug in your code. Don't over-complicate it. Yes there are often many ways to solve a problem, but I'd recommend going with the most straightforward that is correct and fast enough. Don't take a simple problem and try to make it difficult.
Check input and AC output for thousands of problems on uDebug!

uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: I need help with this problem can anyone help me please

Post by uDebug »

brianfry713 wrote:Don't over-complicate it. Yes there are often many ways to solve a problem, but I'd recommend going with the most straightforward that is correct and fast enough. Don't take a simple problem and try to make it difficult.
Thank you for sharing your thoughts.

I'm indeed trying hard to simplify the manner in which I approach a problem but it's not always easy. In other words, I don't complicate the problem on purpose - it's just that I don't necessarily know any other way to go about solving it. I'm hopeful that this will change as I attempt and solve more problems. If you have any thoughts on how I could actively do this (other than keep solving problems), I'd appreciate this very much.

Thanks again.
Last edited by uDebug on Wed Feb 19, 2014 9:29 am, edited 2 times in total.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: I need help with this problem can anyone help me please

Post by brianfry713 »

After reading the problem statement think about what it's asking you to do at a high simple level. Some problem statements are long, verbose, and intentionally misleading but are actually easy to code once you figure it out. On this problem it's just asking you to read in some records and display them sorted.
Check input and AC output for thousands of problems on uDebug!

uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: I need help with this problem can anyone help me please

Post by uDebug »

brianfry713 wrote:After reading the problem statement think about what it's asking you to do at a high simple level. Some problem statements are long, verbose, and intentionally misleading but are actually easy to code once you figure it out. On this problem it's just asking you to read in some records and display them sorted.
Thank you. I tried this approach on a couple problems and it really does help. For example, 11136 (Hoax or What) looks like it's really hard (because it's so long-winded) but like you said, it's not so once you think about it for a bit. I remember that when I first started on UVA back in '10, I couldn't do some of the problems because they were so long-winded / confusing. It didn't occur to me that this was a strategy.

Also, after I work the problem out on scratch paper, I actually write the code down too (and try to make it so that it's bug-free when it's compiled). Then, all I do is type in the code. Sometimes, when I do this, I make adjustments to the original code. If there's too big a variation between the code on scratch paper and the one I finally have when it's all typed in, then things tend to get big / messy. I'm working on them being the same but it's work in progress.

How do you go about solving problems?
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: I need help with this problem can anyone help me please

Post by brianfry713 »

I usually just read the problem statement and then start coding. Sometimes it helps to draw a diagram on paper.
Check input and AC output for thousands of problems on uDebug!

uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: I need help with this problem can anyone help me please

Post by uDebug »

brianfry713 wrote:I usually just read the problem statement and then start coding. Sometimes it helps to draw a diagram on paper.
Thanks for sharing.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

Post Reply

Return to “Volume 4 (400-499)”