import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * Created by stawicad on 2016-11-27. */ public class cnf { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); StringBuilder sb = new StringBuilder(); while (sc.hasNext()){ sb.append(sc.next()); } List<List<Integer>> expression = new ArrayList<>(); List<Integer> current = new ArrayList<>(); List<List<Boolean>> signs = new ArrayList<>(); List<Boolean> signsCurrent = new ArrayList<>(); int prev = 0; boolean not = false; for (char c : sb.toString().toCharArray()) { if(c == '^') { expression.add(current); signs.add(signsCurrent); signsCurrent = new ArrayList<>(); current = new ArrayList<>(); } else if(prev > 0) { if(c >= '0' && c <= '9') current.add(prev * 10 + (c - '0') - 1); else current.add(prev - 1); prev = 0; signsCurrent.add(not); not = false; } else if(c >= '0' && c <= '9') { prev = c - '0'; } else if(c == '~') { not = true; } } expression.add(current); signs.add(signsCurrent); int answer = 0; for (int mask = 0; mask < 1 << n; mask++) { boolean value = true; for (int i = 0; i < expression.size(); i++) { boolean value2 = false; for (int j = 0; j < expression.get(i).size(); j++) { boolean value3 = (mask >> expression.get(i).get(j) & 1) == 1; if(signs.get(i).get(j)) value3 = !value3; value2 = value2 || value3; } value = value &&value2; } if(value)answer++; } System.out.println(answer); } }
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 | import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * Created by stawicad on 2016-11-27. */ public class cnf { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); StringBuilder sb = new StringBuilder(); while (sc.hasNext()){ sb.append(sc.next()); } List<List<Integer>> expression = new ArrayList<>(); List<Integer> current = new ArrayList<>(); List<List<Boolean>> signs = new ArrayList<>(); List<Boolean> signsCurrent = new ArrayList<>(); int prev = 0; boolean not = false; for (char c : sb.toString().toCharArray()) { if(c == '^') { expression.add(current); signs.add(signsCurrent); signsCurrent = new ArrayList<>(); current = new ArrayList<>(); } else if(prev > 0) { if(c >= '0' && c <= '9') current.add(prev * 10 + (c - '0') - 1); else current.add(prev - 1); prev = 0; signsCurrent.add(not); not = false; } else if(c >= '0' && c <= '9') { prev = c - '0'; } else if(c == '~') { not = true; } } expression.add(current); signs.add(signsCurrent); int answer = 0; for (int mask = 0; mask < 1 << n; mask++) { boolean value = true; for (int i = 0; i < expression.size(); i++) { boolean value2 = false; for (int j = 0; j < expression.get(i).size(); j++) { boolean value3 = (mask >> expression.get(i).get(j) & 1) == 1; if(signs.get(i).get(j)) value3 = !value3; value2 = value2 || value3; } value = value &&value2; } if(value)answer++; } System.out.println(answer); } } |