SavingUniverse (Java)

From LiteratePrograms

Jump to: navigation, search

This program is a code dump.
Code dumps are articles with little or no documentation or rearrangement of code. Please help to turn it into a literate program. Also make sure that the source of this code does consent to release it under the MIT or public domain license.

<<SavingUniverse.java>>==
imports
SavingUniverse class


<<imports>>=
import java.io.*;
import java.util.*;
<SavingUniverse class>>=
class SavingUniverse {
 private static BufferedReader stdin =
   new BufferedReader( new InputStreamReader( System.in ) );
 Furthest Engine Method
 Required Switches Method
 Main Method
}
<<Furthest Engine Method>>=
 static int furthest( Set<String> engines , List<String> queries ) {
  int furthest = 0;
  for ( String engine : engines ) {
   int distance;
   distance=queries.indexOf(engine);
   if (distance == -1 ) {
    // Special case. If no match for this engine, it's the furthest.
    return queries.size();
   } else if ( distance > furthest ) {
    furthest = distance;
   }
  }
  return furthest;
 }
<<Required Switches Method>>=
 static int requiredswitches ( Set<String> engines , List<String> queries ) {
  int switches=-1;
  int delbefore;
  do {
   delbefore = furthest(engines, queries);
   System.err.println("Furthest engine is at: " + delbefore );
   for (int delcount=1; delcount <= delbefore ; delcount++ ) {
    System.err.println("Deleting item: " + queries.get(0) );
    queries.remove(0);
   }
   switches++;
  } while ( queries.size() != 0 );
  return switches;
 }
<<Main Method>>=
 public static void main(String args[]) throws IOException {
  int cases;
  int switches;
  String aline;
  String [] parts;
  cases = Integer.parseInt(stdin.readLine());
  for ( int currentCase=1 ; currentCase <= cases ; currentCase++ ) {
   int engineLines;
   int queryLines;
   Set<String> engines = new HashSet<String>();
   List<String> queries = new Vector<String>();
   // Load engines
   engineLines = Integer.parseInt(stdin.readLine());
   for ( int currentEngine=1 ; currentEngine <= engineLines ; currentEngine++ ) {
    aline = stdin.readLine();
    engines.add(aline);
    System.err.println("Adding engine: " + aline);
   }
   // Load queries
   queryLines = Integer.parseInt(stdin.readLine());
   for ( int currentQuery=1 ; currentQuery <= queryLines; currentQuery++ ) {
    aline = stdin.readLine();
    queries.add(aline);
    System.err.println("Adding query: " + aline);
   }
   System.out.println("Case #" + currentCase + ": " +
     requiredswitches(engines, queries));
  }
 }
Download code
Views