コード例 #1
0
ファイル: Algorithm.java プロジェクト: ezhov-evgeny/STopCoder
 void addSuffixes(String word, int id) {
   Node node = children.get(word.charAt(0));
   if (node == null) {
     node = new Node(word.charAt(0), id);
     node.parent = this;
     children.put(word.charAt(0), node);
   }
   if (word.substring(1).length() > 0) {
     node.addSuffixes(word.substring(1), id);
   }
 }
コード例 #2
0
ファイル: Algorithm.java プロジェクト: ezhov-evgeny/STopCoder
  public void solution(String inputFilePath, String usrOutputFilePath) throws IOException {
    Scanner scanner = null;
    FileWriter writer = null;
    try {
      scanner = new Scanner(new File(inputFilePath));
      testCaseNumber = scanner.nextInt();
      writer = new FileWriter(usrOutputFilePath);

      for (int i = 0; i < testCaseNumber; i++) {
        int shapes = scanner.nextInt();
        int number = scanner.nextInt();
        writer.write("Case# " + (i + 1) + "\r\n");
        Node root = new Node();

        scanner.nextLine();
        for (int w = 1; w <= number; w++) {
          String word = scanner.nextLine();
          for (int j = 0; j < word.length(); j++) {
            root.addSuffixes(word.substring(j), w);
          }
        }

        String previous = "";
        String current = "";
        int index = 0;
        do {
          previous = current;
          current = root.findNearest(previous, index, shapes);
          index++;
        } while (current != null && index != 60);

        writer.write(((previous.length() > 0) ? "" + previous.length() : "No") + "\r\n");
      }

    } finally {
      if (writer != null) {
        writer.close();
      }
      if (scanner != null) {
        scanner.close();
      }
    }
  }