Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.Scanner;
  4.  
  5. public class Main
  6. {
  7. public static ArrayList<String> ruleList = new ArrayList<String>();
  8.  
  9. public static void main(String[] args)
  10. {
  11. Scanner scan = new Scanner(System.in);
  12.  
  13. for(int i = 0; i < 3; i++)
  14. {
  15. ruleList.add(scan.nextLine());
  16. }
  17. String instruction = scan.nextLine();
  18.  
  19. String[] tokens = instruction.split(" ");
  20.  
  21. String input = tokens[1];
  22. for(String s: findAllPossibilities(tokens[1] + ",", ruleList, Integer.parseInt(tokens[0])))
  23. {
  24. input = tokens[1];
  25. String[] split = s.split(",");
  26.  
  27. if(split[0].equals(tokens[2]))
  28. {
  29. String[] steps = split[1].split(":");
  30.  
  31. for(int i = 0; i < steps.length; i+= 2)
  32. {
  33. input = useRule(input, ruleList.get(Integer.parseInt(steps[i])), Integer.parseInt(steps[i+1]));
  34. System.out.println((Integer.parseInt(steps[i]) + 1) + " " + (Integer.parseInt(steps[i+1]) + 1) + " " + input);
  35. }
  36.  
  37. System.exit(0);
  38. }
  39. }
  40.  
  41.  
  42. }
  43.  
  44. public static ArrayList<String> findPossibilities(String input, ArrayList<String> rules)
  45. {
  46. ArrayList<String> possibilities = new ArrayList<String>();
  47.  
  48. for(String s: rules)
  49. {
  50. String[] tokens = s.split(" ");
  51.  
  52. if(input.contains(tokens[0]))
  53. {
  54. for(int i : getIndexes(input, tokens[0]))
  55. {
  56. possibilities.add(rules.indexOf(s) + "," + i);
  57. }
  58. }
  59. }
  60.  
  61. return possibilities;
  62. }
  63.  
  64. public static ArrayList<Integer> getIndexes(String input, String test)
  65. {
  66. ArrayList<Integer> indexes = new ArrayList<Integer>();
  67.  
  68. int startIndex = 0;
  69.  
  70. for(int i = 0; i < input.length(); i++)
  71. {
  72. if(input.indexOf(test, startIndex) != -1)
  73. {
  74. indexes.add(input.indexOf(test, startIndex));
  75. startIndex = input.indexOf(test, startIndex) + 1;
  76. }
  77. }
  78.  
  79. return indexes;
  80. }
  81.  
  82. public static String useRule(String input, String rule, int index)
  83. {
  84. String[] tokens = rule.split(" ");
  85.  
  86. String firstpart = input.substring(0, index);
  87. String secondpart = input.substring(index + tokens[0].length());
  88. return firstpart + tokens[1] + secondpart;
  89. }
  90.  
  91. public static ArrayList<String> findAllPossibilities(String input, ArrayList<String> rules, int depth)
  92. {
  93. ArrayList<String> allPossibilities = new ArrayList<String>();
  94. ArrayList<String> newItems = new ArrayList<String>();
  95. ArrayList<String> itemsToTest = new ArrayList<String>();
  96.  
  97. itemsToTest.add(input);
  98.  
  99. for(int i = 0; i < depth; i++)
  100. {
  101. for(String test: itemsToTest)
  102. {
  103. for(String s: findPossibilities(test, rules))
  104. {
  105. //System.out.println(itemsToTest);
  106. //System.out.println(findPossibilities(test, rules) + " possibltiltity");
  107.  
  108. String[] tokens = s.split(",");
  109. String[] splitTest = test.split(",");
  110. //System.out.println(test + " " + ruleList.get(Integer.parseInt(tokens[0])) + " " + Integer.parseInt(tokens[1]));
  111.  
  112. if(i > 0)
  113. {
  114. newItems.add(useRule(splitTest[0], ruleList.get(Integer.parseInt(tokens[0])), Integer.parseInt(tokens[1])) + "," + splitTest[1] + tokens[0] + ":" + tokens[1] + ":");
  115. }
  116. else
  117. {
  118. newItems.add(useRule(splitTest[0], ruleList.get(Integer.parseInt(tokens[0])), Integer.parseInt(tokens[1])) + "," + tokens[0] + ":" + tokens[1] + ":");
  119. }
  120. }
  121. }
  122.  
  123. itemsToTest.clear();
  124. itemsToTest.addAll(newItems);
  125. newItems.clear();
  126. }
  127.  
  128. return itemsToTest;
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement