- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- 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.
Use this to learn the idea, then write your own version.
123 456 789101112131415161718 192021222324import java.awt.*;25import hsa.*;26 27public class P3bitpattern28{29 static Console c; 30 31 public static void main (String [] args)32 {33 c = new Console ();34 35 TextInputFile fin = new TextInputFile ("pat.in");36 TextOutputFile fout = new TextOutputFile ("pat.out");37 String s;38 StringBuffer b;39 int number, i, k, n, x;40 41 number = fin.readInt ();42 for (int j = 0 ; j < number ; j++)43 {44 45 46 n = fin.readInt ();47 k = fin.readInt ();48 s = "";49 for (i = 0 ; i < k ; i++)50 s = s + "1";51 for (; i < n ; i++)52 s = s + "0";53 54 55 56 x = s.lastIndexOf ("10");57 fout.println ("The bit patterns are: ");58 c.println ("The bit patterns are: ");59 while (x >= 0)60 {61 fout.println (s);62 c.println (s);63 b = new StringBuffer (s.substring (x + 2));64 s = s.substring (0, x) + "01" + b.reverse ();65 x = s.lastIndexOf ("10");66 }67 c.println (s);68 c.println ("\n");69 fout.println (s);70 fout.println ("\n");71 }72 fout.close ();73 fin.close ();74 }75}76