Problem solution · Java

CCC 2007 S3 - Friends

CCC 2007 S3 - Friends: a Java solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Direct simulation
Source
CCCSolutions
Length
96 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For CCC 2007 S3 - Friends, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 96 lines of Java from the credited upstream file ccc07s3.java.
  • The implementation visibly relies on sequence storage.
  • 4 loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from CCCSolutions by CCCSolutions contributors · Milliken Mills High School and is used under the MIT licence.

Full codeCCC 2007 S3 - Friends · JavaJava
Use this to learn the idea, then write your own version.
// CCC 2007//// S3: Friends//// This is a relatively straight forward array processing exercise,// with a bit of a twist.//// number is an array of student numbers (not sorted)//     (they MIGHT be anything so strings are used)// friend is an array of friends :-)//     (that is friend[5] is the friend of number[5])//// SO the real issue is given a student number, find its location// in the number array, and then use that to see who the friend is,// using the friend array. Cycle thru this until you get back to// the original person or the one you're looking for. That will give// you the separation, if you get back to the second person. (If you// end up back at the start, they are not in the same circle.)//// Data file 4 is tricky: there are student numbers// NOT in the school so you need to watch out for that.//// Note: searching is linear and slow.// But it still runs within 10 seconds.// import java.awt.*;import hsa.*; public class CCC2007S3Friends{    static Console c;    static int n, i, size, distance;    static String s1, s2, x;    static String[] friend;    static String[] number;     public static void main (String[] args)    {	c = new Console ();	TextInputFile f = new TextInputFile ("s3.2.in"); 	// get the friend relationships	n = f.readInt ();	friend = new String [n];	number = new String [n];	size = n;	for (int i = 0 ; i < n ; i++)	{	    s1 = f.readString ();	    s2 = f.readString ();	    number [i] = s1;	    friend [i] = s2;	} 	// get the student numbers and determine relationship	// watch out for invalid student numbers.	s1 = f.readString ();	s2 = f.readString ();	while (!(s1.equals ("0") && s2.equals ("0")))	{	    x = s1;	    i = find (x);	    distance = 0;	    while (i >= 0 && !(friend [i].equals (s1) || friend [i].equals (s2)))	    {		distance++;		x = friend [i];		i = find (x);	    }	    if (i >= 0 && friend [i].equals (s2))		c.println ("Yes " + distance);	    else		c.println ("No");	    s1 = f.readString ();	    s2 = f.readString ();	}    }      // this returns the location of s in the number array    // -1 if not found.    public static int find (String s)    {	int i;	i = 0;	while (i < size && !number [i].equals (s))	    i++;	if (i < size)	    return i;	else	    return -1;     }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗