import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class cnf { public static void main(String args[]){ Scanner sc = new Scanner(System.in); int expressionCount = sc.nextInt(); sc.nextLine(); String line = sc.nextLine(); line = line .replace(" ","") .replace("(","") .replace(")","") .replace("x",""); String[] expressionsString = line.split("\\^"); //Nr of exp | Content | Count int[][][] expressions = new int[expressionCount][expressionCount][expressionCount]; for(int i = 0; i<expressionsString.length; i++){ String[] a = expressionsString[i].split("v"); for(int j = 0; j< a.length; j++){ if(a[j].contains("~")){ expressions[i][j][0] = Integer.parseInt(a[j].substring(1))*-1; } else { expressions[i][j][0] = Integer.parseInt(a[j]); } expressions[i][j][1] = a.length; } } Arrays.sort(expressions, new Comparator<int[][]>() { @Override public int compare(int[][] o1, int[][] o2) { return Integer.compare(o1[0][1], o2[0][1]); } }); int[] results = new int[expressionCount+1]; for(int i = 0; i < expressions.length; i++){ if(expressions[i][0][1] == 1){ if(expressions[i][0][0] > 0){ results[expressions[i][0][0]]=1; } else { results[expressions[i][0][0]*-1]=-1; } } else { checkExpression(results, expressions[i]); } } int counter = 1; for(int i = 1; i < results.length; i++){ if(results[i]==0){ counter*=2; } } System.out.println(counter); } private static void checkExpression(int[] results, int[][] expression) { boolean current = false; int candidate = 0; for(int i = 0; i < expression.length; i++){ if(expression[i][0] > 0){ if(results[expression[i][0]] != 0){ if(results[expression[i][0]] == 1){ current = true; } } else { if(candidate == 0) { candidate = expression[i][0]; } else { if (current) { if(candidate < 0) { results[expression[i][0]] = -1; } else { results[expression[i][0]] = 1; } } else { results[expression[i][0]] = 0; } } } } else if(expression[i][0] < 0) { if(results[expression[i][0] * -1] != 0){ current = false; } else { if(candidate == 0) { candidate = expression[i][0] * -1; } else { if(current){ if(candidate < 0) { results[expression[i][0]*-1] = -1; } else { results[expression[i][0]*-1] = 1; } } else { results[expression[i][0] * -1] = 0; } } } } } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class cnf { public static void main(String args[]){ Scanner sc = new Scanner(System.in); int expressionCount = sc.nextInt(); sc.nextLine(); String line = sc.nextLine(); line = line .replace(" ","") .replace("(","") .replace(")","") .replace("x",""); String[] expressionsString = line.split("\\^"); //Nr of exp | Content | Count int[][][] expressions = new int[expressionCount][expressionCount][expressionCount]; for(int i = 0; i<expressionsString.length; i++){ String[] a = expressionsString[i].split("v"); for(int j = 0; j< a.length; j++){ if(a[j].contains("~")){ expressions[i][j][0] = Integer.parseInt(a[j].substring(1))*-1; } else { expressions[i][j][0] = Integer.parseInt(a[j]); } expressions[i][j][1] = a.length; } } Arrays.sort(expressions, new Comparator<int[][]>() { @Override public int compare(int[][] o1, int[][] o2) { return Integer.compare(o1[0][1], o2[0][1]); } }); int[] results = new int[expressionCount+1]; for(int i = 0; i < expressions.length; i++){ if(expressions[i][0][1] == 1){ if(expressions[i][0][0] > 0){ results[expressions[i][0][0]]=1; } else { results[expressions[i][0][0]*-1]=-1; } } else { checkExpression(results, expressions[i]); } } int counter = 1; for(int i = 1; i < results.length; i++){ if(results[i]==0){ counter*=2; } } System.out.println(counter); } private static void checkExpression(int[] results, int[][] expression) { boolean current = false; int candidate = 0; for(int i = 0; i < expression.length; i++){ if(expression[i][0] > 0){ if(results[expression[i][0]] != 0){ if(results[expression[i][0]] == 1){ current = true; } } else { if(candidate == 0) { candidate = expression[i][0]; } else { if (current) { if(candidate < 0) { results[expression[i][0]] = -1; } else { results[expression[i][0]] = 1; } } else { results[expression[i][0]] = 0; } } } } else if(expression[i][0] < 0) { if(results[expression[i][0] * -1] != 0){ current = false; } else { if(candidate == 0) { candidate = expression[i][0] * -1; } else { if(current){ if(candidate < 0) { results[expression[i][0]*-1] = -1; } else { results[expression[i][0]*-1] = 1; } } else { results[expression[i][0] * -1] = 0; } } } } } } } |