Advertisement
vladimirVenkov

Expressions Again with Grey Code Solution

Jul 1st, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.57 KB | None | 0 0
  1. /*
  2. Coki loves numbers. Yet, he cannot use them. Alas, he is that smart...
  3.  
  4. Now he wants to calculate expressions! He has digits and a number. His task is to generate all valid mathematical
  5. expressions, that can be can be done with the digits by inserting operators '+', '*' or '-', between the digits,
  6. excluding at the begining and the end.
  7.  
  8. The expressions are calculated as the calculator does, i.e. 2 + 3 * 5 = 5 * 5 = 25, not 2 + 15 = 17
  9.  
  10. Example:
  11.  
  12. From 123 the valid expressions are:
  13. 1*2*3 = 6
  14. 1*2+3 = 5
  15. 1*2-3 = -1
  16. 1*23  = 23
  17. 1+2*3 = 9
  18. 1+2+3 = 6
  19. 1+2-3 = 0
  20. 1+23  = 24
  21. 1-2*3 = -3
  22. 1-2+3 = 2
  23. 1-2-3 = -4
  24. 1-23  = -22
  25. 12*3  = 36
  26. 12+3  = 15
  27. 12-3  = 9
  28. 123   = 123
  29. Help Coki to count the expressions, that evaluate to the provided number
  30.  
  31. Input
  32. The input will be on the standart input
  33.  
  34. On the first line of the input you will find the sequence of digits
  35. On the second line of the input, you will find the number
  36. The input will be valid and there is no need to check it explicitly
  37.  
  38. Output
  39. Print the output on the standart output
  40.  
  41. On the first line print the number K
  42.  
  43. The count of possible valid expressions
  44. Constraints
  45. The digits will always be at most 14
  46.  
  47. 123
  48. 6
  49. output: 2
  50.  
  51. 105
  52. 5
  53. output: 4
  54.  
  55. 000
  56. 0
  57. output: 9
  58.  
  59. 1111
  60. 1
  61. output: 7
  62.  
  63.  */
  64.  
  65.  
  66. package SomeNewTasksAndSomeOld;
  67.  
  68. import java.io.BufferedReader;
  69. import java.io.IOException;
  70. import java.io.InputStreamReader;
  71. import java.util.ArrayList;
  72.  
  73. public class ExpressionsAgain {
  74.     public static void main(String[] args) throws IOException {
  75.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  76.         String input = br.readLine();
  77.         int length = input.length();
  78.         int[] numbers = new int [length];
  79.  
  80.         for (int i = 0; i < length; i++) {
  81.             numbers[i] = input.charAt(i) - '0';
  82.         }
  83.         int targetSum = Integer.parseInt(br.readLine());
  84.         ArrayList<ArrayList<Integer>> greyCode = new ArrayList<>();
  85.         greyCodeGenerator(length - 1, new ArrayList<>(), greyCode);
  86.         ArrayList<ArrayList<Integer>> concatenations = new ArrayList<>();
  87.         concatenator(numbers, greyCode, concatenations);
  88.  
  89.         for (ArrayList<Integer> element : concatenations) {
  90.             solve(0, element,element.get(0), targetSum);
  91.         }
  92.         System.out.println(counter);
  93.     }
  94.    
  95.     //todo Generate grey code from 0000 to 1111 (for depth 4)
  96.     private static void greyCodeGenerator(int depth, ArrayList<Integer> crnt, ArrayList<ArrayList<Integer>> greyCode) {
  97.         if (depth == 0) {
  98.             ArrayList<Integer> toAdd = new ArrayList<>(crnt);
  99.             greyCode.add(toAdd);
  100.             return;
  101.         }
  102.         crnt.add(0);
  103.         greyCodeGenerator(depth - 1, crnt, greyCode);
  104.         crnt.set(crnt.size() - 1, 1);
  105.         greyCodeGenerator(depth - 1, crnt, greyCode);
  106.         crnt.remove(crnt.size() - 1);
  107.  
  108.     }
  109.     //todo generate all possible different concatenations using greycode source
  110.     // todo (1 means concatenate and zero keep them separate)
  111.     //todo Includes validation for zero in front
  112.     private static void concatenator(int[] arry, ArrayList<ArrayList<Integer>> grey,
  113.                                      ArrayList<ArrayList<Integer>> result) {
  114.         for (ArrayList<Integer> element : grey) {
  115.             boolean isValid = true;
  116.             ArrayList<Integer> temp = new ArrayList<>();
  117.             temp.add(arry[0]);
  118.             for (int i = 0; i < element.size(); i++) {
  119.  
  120.                 if (element.get(i) == 0) temp.add(arry[i + 1]);
  121.  
  122.                 else if (temp.get(temp.size() - 1) == 0) { //if last number is zero don't concatenate
  123.                     isValid = false;
  124.                     break;
  125.                 }
  126.  
  127.                 else temp.set(temp.size() - 1, temp.get(temp.size() - 1) * 10 + arry[i + 1]);
  128.             }
  129.             if(isValid) result.add(temp);
  130.         }
  131.     }
  132.     //todo solution for the 3 possible signs (+, - , * )
  133.     static int counter = 0;
  134.     static void solve(int index, ArrayList<Integer> numbers, int crnt, int target) {
  135.         int length = numbers.size();
  136.         //stop
  137.         if (index == length - 1) {
  138.             if (target == crnt) {
  139.                 counter++;
  140.                 return;
  141.             }
  142.             return;
  143.         }
  144.  
  145.         int one = crnt + numbers.get(index + 1);
  146.         solve(index + 1, numbers, one, target );
  147.  
  148.         int two = crnt - numbers.get(index + 1);
  149.         solve(index + 1, numbers, two, target);
  150.  
  151.         int three = crnt * numbers.get(index + 1);
  152.         solve(index + 1, numbers, three, target);
  153.  
  154.     }
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement