Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Coki loves numbers. Yet, he cannot use them. Alas, he is that smart...
- Now he wants to calculate expressions! He has digits and a number. His task is to generate all valid mathematical
- expressions, that can be can be done with the digits by inserting operators '+', '*' or '-', between the digits,
- excluding at the begining and the end.
- The expressions are calculated as the calculator does, i.e. 2 + 3 * 5 = 5 * 5 = 25, not 2 + 15 = 17
- Example:
- From 123 the valid expressions are:
- 1*2*3 = 6
- 1*2+3 = 5
- 1*2-3 = -1
- 1*23 = 23
- 1+2*3 = 9
- 1+2+3 = 6
- 1+2-3 = 0
- 1+23 = 24
- 1-2*3 = -3
- 1-2+3 = 2
- 1-2-3 = -4
- 1-23 = -22
- 12*3 = 36
- 12+3 = 15
- 12-3 = 9
- 123 = 123
- Help Coki to count the expressions, that evaluate to the provided number
- Input
- The input will be on the standart input
- On the first line of the input you will find the sequence of digits
- On the second line of the input, you will find the number
- The input will be valid and there is no need to check it explicitly
- Output
- Print the output on the standart output
- On the first line print the number K
- The count of possible valid expressions
- Constraints
- The digits will always be at most 14
- 123
- 6
- output: 2
- 105
- 5
- output: 4
- 000
- 0
- output: 9
- 1111
- 1
- output: 7
- */
- package SomeNewTasksAndSomeOld;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- public class ExpressionsAgain {
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String input = br.readLine();
- int length = input.length();
- int[] numbers = new int [length];
- for (int i = 0; i < length; i++) {
- numbers[i] = input.charAt(i) - '0';
- }
- int targetSum = Integer.parseInt(br.readLine());
- ArrayList<ArrayList<Integer>> greyCode = new ArrayList<>();
- greyCodeGenerator(length - 1, new ArrayList<>(), greyCode);
- ArrayList<ArrayList<Integer>> concatenations = new ArrayList<>();
- concatenator(numbers, greyCode, concatenations);
- for (ArrayList<Integer> element : concatenations) {
- solve(0, element,element.get(0), targetSum);
- }
- System.out.println(counter);
- }
- //todo Generate grey code from 0000 to 1111 (for depth 4)
- private static void greyCodeGenerator(int depth, ArrayList<Integer> crnt, ArrayList<ArrayList<Integer>> greyCode) {
- if (depth == 0) {
- ArrayList<Integer> toAdd = new ArrayList<>(crnt);
- greyCode.add(toAdd);
- return;
- }
- crnt.add(0);
- greyCodeGenerator(depth - 1, crnt, greyCode);
- crnt.set(crnt.size() - 1, 1);
- greyCodeGenerator(depth - 1, crnt, greyCode);
- crnt.remove(crnt.size() - 1);
- }
- //todo generate all possible different concatenations using greycode source
- // todo (1 means concatenate and zero keep them separate)
- //todo Includes validation for zero in front
- private static void concatenator(int[] arry, ArrayList<ArrayList<Integer>> grey,
- ArrayList<ArrayList<Integer>> result) {
- for (ArrayList<Integer> element : grey) {
- boolean isValid = true;
- ArrayList<Integer> temp = new ArrayList<>();
- temp.add(arry[0]);
- for (int i = 0; i < element.size(); i++) {
- if (element.get(i) == 0) temp.add(arry[i + 1]);
- else if (temp.get(temp.size() - 1) == 0) { //if last number is zero don't concatenate
- isValid = false;
- break;
- }
- else temp.set(temp.size() - 1, temp.get(temp.size() - 1) * 10 + arry[i + 1]);
- }
- if(isValid) result.add(temp);
- }
- }
- //todo solution for the 3 possible signs (+, - , * )
- static int counter = 0;
- static void solve(int index, ArrayList<Integer> numbers, int crnt, int target) {
- int length = numbers.size();
- //stop
- if (index == length - 1) {
- if (target == crnt) {
- counter++;
- return;
- }
- return;
- }
- int one = crnt + numbers.get(index + 1);
- solve(index + 1, numbers, one, target );
- int two = crnt - numbers.get(index + 1);
- solve(index + 1, numbers, two, target);
- int three = crnt * numbers.get(index + 1);
- solve(index + 1, numbers, three, target);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement