Problem solution · Java

CCC 1996 P3 - Pattern Generator

CCC 1996 P3 - Pattern Generator: 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
76 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 1996 P3 - Pattern Generator, 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

  • 76 lines of Java from the credited upstream file ccc96s3.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 1996 P3 - Pattern Generator · JavaJava
Use this to learn the idea, then write your own version.
// CCC 1996// Problem C: Pattern Generator // Given k and n, print all bit patterens of k 1's in bit strings of length n// in descending order // Eg: k = 2 n = 5// 11000// 10100// 10010// 10001// 01100// 01010// 01001// 00110// 00101// 00011 // There is "trick" to this:// a. create the first string (easy)// b. for ALL other strings find the LAST "10" and reverse it, AND//                 reverse the right part of the sring AFTER the "10"//import java.awt.*;import hsa.*; public class P3bitpattern{    static Console c;           // The output console     public static void main (String [] args)    {	c = new Console (); 	TextInputFile fin = new TextInputFile ("pat.in");	TextOutputFile fout = new TextOutputFile ("pat.out");	String s;	StringBuffer b;	int number, i, k, n, x; 	number = fin.readInt ();	for (int j = 0 ; j < number ; j++)	{ 	    // read and create the original string	    n = fin.readInt ();	    k = fin.readInt ();	    s = "";	    for (i = 0 ; i < k ; i++)		s = s + "1";	    for (; i < n ; i++)		s = s + "0"; 	    // find the last "10", reverse that AND	    //       reverse the part to the right of it	    x = s.lastIndexOf ("10");	    fout.println ("The bit patterns are: ");	    c.println ("The bit patterns are: ");	    while (x >= 0)	    {		fout.println (s);		c.println (s);		b = new StringBuffer (s.substring (x + 2));		s = s.substring (0, x) + "01" + b.reverse ();		x = s.lastIndexOf ("10");	    }	    c.println (s);	    c.println ("\n");	    fout.println (s);	    fout.println ("\n");	}	fout.close ();	fin.close ();    }} 

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 ↗