A
Albert
As a response to the following problem,
Speed Cameras
Input File: camin.txt
Output File: camout.txt
Time Limit: 1 second
As director of the Safe Driving Ministry, you are faced with a dilemma.
None of your speed cameras are working, but your department desperately
needs the extra revenue for their new Super Espresso Coffee Deluxe. With
determination, you and your co-workers head down to the highway to
balance the budget.
You order your officers Bernard and Peter to stand at either end of a
tunnel running through a hill. Bernard records the order in which cars
enter the tunnel, and Peter records the order in which cars exit the
tunnel. There is a strict no-overtaking rule in the tunnel, so given
this information you can reliably pull over and fine a certain number of
drivers at your roadblock further down the highway.
Given Bernard's and Peter's lists, you must write a program that
determines how many cars you can claim made an illegal overtake with
certainty.
Input
The first line of input will contain a single integer N representing the
number of cars that drove through the tunnel ( 1 <= N <= 1000).
Following this will be N lines describing Bernard's list. Each line will
contain the number plate of a car that entered the tunnel, in order from
first entry to last entry. Following this will be N lines describing
Peter's list, each line containing the number plate of a car that exited
the tunnel in order from first exit to last exit.
Each number plate consists of at least six and at most eight characters.
Only capital letters (A-Z) and digits (0-9) will be used. No two cars
will have the same number plate.
Output
Output should consist of a single integer representing how many drivers
you know with certainty have made an overtake.
Sample Input 1
4
109DLY
SSH2ANU
VOLVO76
REDCAR
REDCAR
109DLY
SSH2ANU
VOLVO76
Sample Output 1
1
Sample Input 2
5
TRS80MCX
IOI2004
DEB18N
REDCAR
REGINA41
IOI2004
REGINA41
REDCAR
TRS80MCX
DEB18N
Sample Output 2
3
Sample Input 3
5
REDCAR
L0LL1P0P
UQ9396
109DLY
B1LKENT
B1LKENT
UQ9396
REDCAR
L0LL1P0P
109DLY
Sample Output 3
2
I'm getting a seg fault with Input 2 and
/* This program reads in two lists of car licence plates and uses this
to determine how many cars have overtaken *with certainty*. It simulates
the overtakings starting with the car at the top of out list, modifying
the in list until the bottom of the out list is reached. */
#include <stdio.h>
#include <string.h>
enum { MAXCARS = 1000 };
FILE *in, *out;
int ncars;
char *inlist[MAXCARS], *outlist[MAXCARS]; /* stores the list of license
plates before the cars enter the tunnel and after. */
int find(char *s)
{
int i;
for (i = 1; i <= ncars; i++)
if (strcmp(inlist, s) == 0)
return i;
}
int main(void)
{
in = fopen("speedin.txt", "r"); /* CHECK */
out = fopen("speedout.txt", "w");
fscanf(in, "%d", &ncars);
int i;
for (i = 1; i <= ncars; i++)
fscanf(in, "%s", inlist);
printf("%d\n", find("TRS80MCX"));
return 0;
}
Running it under GDB reveals an issue with strcmp...What is wrong?
Speed Cameras
Input File: camin.txt
Output File: camout.txt
Time Limit: 1 second
As director of the Safe Driving Ministry, you are faced with a dilemma.
None of your speed cameras are working, but your department desperately
needs the extra revenue for their new Super Espresso Coffee Deluxe. With
determination, you and your co-workers head down to the highway to
balance the budget.
You order your officers Bernard and Peter to stand at either end of a
tunnel running through a hill. Bernard records the order in which cars
enter the tunnel, and Peter records the order in which cars exit the
tunnel. There is a strict no-overtaking rule in the tunnel, so given
this information you can reliably pull over and fine a certain number of
drivers at your roadblock further down the highway.
Given Bernard's and Peter's lists, you must write a program that
determines how many cars you can claim made an illegal overtake with
certainty.
Input
The first line of input will contain a single integer N representing the
number of cars that drove through the tunnel ( 1 <= N <= 1000).
Following this will be N lines describing Bernard's list. Each line will
contain the number plate of a car that entered the tunnel, in order from
first entry to last entry. Following this will be N lines describing
Peter's list, each line containing the number plate of a car that exited
the tunnel in order from first exit to last exit.
Each number plate consists of at least six and at most eight characters.
Only capital letters (A-Z) and digits (0-9) will be used. No two cars
will have the same number plate.
Output
Output should consist of a single integer representing how many drivers
you know with certainty have made an overtake.
Sample Input 1
4
109DLY
SSH2ANU
VOLVO76
REDCAR
REDCAR
109DLY
SSH2ANU
VOLVO76
Sample Output 1
1
Sample Input 2
5
TRS80MCX
IOI2004
DEB18N
REDCAR
REGINA41
IOI2004
REGINA41
REDCAR
TRS80MCX
DEB18N
Sample Output 2
3
Sample Input 3
5
REDCAR
L0LL1P0P
UQ9396
109DLY
B1LKENT
B1LKENT
UQ9396
REDCAR
L0LL1P0P
109DLY
Sample Output 3
2
I'm getting a seg fault with Input 2 and
/* This program reads in two lists of car licence plates and uses this
to determine how many cars have overtaken *with certainty*. It simulates
the overtakings starting with the car at the top of out list, modifying
the in list until the bottom of the out list is reached. */
#include <stdio.h>
#include <string.h>
enum { MAXCARS = 1000 };
FILE *in, *out;
int ncars;
char *inlist[MAXCARS], *outlist[MAXCARS]; /* stores the list of license
plates before the cars enter the tunnel and after. */
int find(char *s)
{
int i;
for (i = 1; i <= ncars; i++)
if (strcmp(inlist, s) == 0)
return i;
}
int main(void)
{
in = fopen("speedin.txt", "r"); /* CHECK */
out = fopen("speedout.txt", "w");
fscanf(in, "%d", &ncars);
int i;
for (i = 1; i <= ncars; i++)
fscanf(in, "%s", inlist);
printf("%d\n", find("TRS80MCX"));
return 0;
}
Running it under GDB reveals an issue with strcmp...What is wrong?