Problem solution · Java

CCC 2005 S1 - Snow Calls

CCC 2005 S1 - Snow Calls: 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
61 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 2005 S1 - Snow Calls, 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

  • 61 lines of Java from the credited upstream file ccc05s1.java.
  • The implementation visibly relies on sequence storage.
  • 2 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 2005 S1 - Snow Calls · JavaJava
Use this to learn the idea, then write your own version.
// The "CCC2005s1snowcalls" class.//// Basic string handling: that and basic structures import java.awt.*;import hsa.*; public class CCC2005s1SnowCalls{    static Console c;     public static void main (String[] args)    {	c = new Console ();	TextInputFile f = new TextInputFile ("s1.in");	int n;	String s; 	n = f.readInt ();	for (int i = 1 ; i <= n ; i++)	    c.println (convert (f.readLine ()));    }     // no checks are made on s as the problem states it     // represents a valid number    public static String convert (String s)    {	String out;	int i;	i = 0;	out = "";	while (out.length () < 12)	{	    if (s.charAt (i) == '1')		out = out + "1";	    else if (s.charAt (i) == '2' || s.charAt (i) == 'A' || s.charAt (i) == 'B' || s.charAt (i) == 'C')		out = out + "2";	    else if (s.charAt (i) == '3' || s.charAt (i) == 'D' || s.charAt (i) == 'E' || s.charAt (i) == 'F')		out = out + "3";	    else if (s.charAt (i) == '4' || s.charAt (i) == 'G' || s.charAt (i) == 'H' || s.charAt (i) == 'I')		out = out + "4";	    else if (s.charAt (i) == '5' || s.charAt (i) == 'J' || s.charAt (i) == 'K' || s.charAt (i) == 'L')		out = out + "5";	    else if (s.charAt (i) == '6' || s.charAt (i) == 'M' || s.charAt (i) == 'N' || s.charAt (i) == 'O')		out = out + "6";	    else if (s.charAt (i) == '7' || s.charAt (i) == 'P' || s.charAt (i) == 'Q' || s.charAt (i) == 'R' || s.charAt (i) == 'S')		out = out + "7";	    else if (s.charAt (i) == '8' || s.charAt (i) == 'T' || s.charAt (i) == 'U' || s.charAt (i) == 'V')		out = out + "8";	    else if (s.charAt (i) == '9' || s.charAt (i) == 'W' || s.charAt (i) == 'X' || s.charAt (i) == 'Y' || s.charAt (i) == 'Z')		out = out + "9";	    else if (s.charAt (i) == '0')		out = out + "0";	    i++;	    if (out.length () == 3 || out.length () == 7)		out = out + "-";	}	return out;    }} 

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 ↗