DSAP3.java


SUBMITTED BY: antfuentes87

DATE: Nov. 25, 2015, 12:50 a.m.

FORMAT: Text only

SIZE: 2.1 kB

HITS: 873

  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.lang.*;
  7. public class DSAP3 {
  8. static ArrayList<ArrayList<Integer>> data = new ArrayList<ArrayList<Integer>>();
  9. public static class Node {
  10. static int id;
  11. static int val;
  12. static ArrayList<Node> app = new ArrayList<Node>();
  13. private void add (Node mag) {
  14. app.add(mag);
  15. }
  16. private Node getChild (int pos) {
  17. return app.get(pos);
  18. }
  19. }
  20. public static void main(String[] args) {
  21. //read txt file
  22. BufferedReader br = null;
  23. try {
  24. int h = 0;
  25. //read in file
  26. String sCurrentLine;
  27. String file_path = args[0];
  28. br = new BufferedReader(new FileReader(file_path));
  29. h = Integer.parseInt(br.readLine());
  30. //read lines
  31. int i = 0;
  32. ArrayList<Integer> apps = new ArrayList<Integer>();
  33. String str;
  34. String[] stra;
  35. while ((sCurrentLine = br.readLine()) != null) {
  36. str = sCurrentLine.replaceAll("\\D+"," ");
  37. str = str + " x";
  38. stra = str.split(" +");
  39. while (!(stra[i]).equals("x")) {
  40. apps.add(Integer.parseInt(stra[i]));
  41. i++;
  42. }
  43. data.add(apps);
  44. apps = new ArrayList<Integer>();
  45. i = 0;
  46. }
  47. Node head = addMag(0);
  48. System.out.println(head.val);
  49. // System.out.println(head.getChild(0).val);
  50. // System.out.println(head.getChild(1).val);
  51. // System.out.println(head.getChild(2).val);
  52. // System.out.println(head.getChild(1).getChild(1).val);
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. public static Node addMag(int x) {
  58. Node temp = new Node();
  59. temp.id = data.get(x).get(0);
  60. temp.val = data.get(x).get(1);
  61. // System.out.print(temp.id + " ");
  62. // System.out.println(temp.val + " ");
  63. for (int c = 2; c < data.get(x).size(); c++) {
  64. temp.add(addMag(data.get(x).get(c)-1));
  65. }
  66. // System.out.print(temp.id + " ");
  67. // System.out.println(temp.val + " ");
  68. // System.out.print("\n");
  69. return temp;
  70. }
  71. }

comments powered by Disqus