import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class cnf { public static void main(String[] args) { cnf element = new cnf(); element.doWork(args); System.exit(0); } public void doWork(String[] args){ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Integer i = null; String inputText = null; try { i = new Integer(br.readLine()); inputText = br.readLine(); } catch (IOException e) { e.printStackTrace(); } int numberOfSuccesses = 0; ListListString elements = new ListListString(); ListListBoolean elementsSign = new ListListBoolean(); boolean[][] possibilities = new boolean[i][(int) Math.pow(2, i)]; ListBoolean pattern; ListBoolean pattern2; for(int indexPreparePossibilities=0; indexPreparePossibilities<possibilities.length;indexPreparePossibilities++){ pattern = new ListBoolean(); for(int indexPattern1=0; indexPattern1<Math.pow(2, indexPreparePossibilities);indexPattern1++) pattern.add(true); for(int indexPattern2=0; indexPattern2<Math.pow(2, indexPreparePossibilities);indexPattern2++) pattern.add(false); pattern2 = new ListBoolean(); for(int indexAntyPattern1=pattern.size(); indexAntyPattern1>0;indexAntyPattern1--) pattern2.add(pattern.get(indexAntyPattern1-1)); for(int j=0;j<possibilities[indexPreparePossibilities].length; j = j + pattern.size()*2){ for(int k = 0; k<pattern.size(); k++){ possibilities[indexPreparePossibilities][j+k]=pattern.get(k); } for(int k = 0; k<pattern.size(); k++){ if(j+pattern.size()+k < possibilities[indexPreparePossibilities].length) possibilities[indexPreparePossibilities][j+pattern.size()+k]=pattern2.get(k); else break; } } } String[] dividedText = divideRows(inputText); for(String text: dividedText){ ListString row = new ListString(); ListBoolean rowSign = new ListBoolean(); String[] textElements = divideElements(text); for(String elementToAdd : textElements){ String preparedElement = prepareElement(elementToAdd); if(preparedElement.startsWith("~")){ rowSign.add(false); row.add(preparedElement.replace("~", "")); } else{ rowSign.add(true); row.add(preparedElement); } } elements.add(row); elementsSign.add(rowSign); } for(int j=0;j<possibilities[0].length;j++){ boolean[] rowResults = new boolean[elements.size()]; for(int k=0;k<elements.size();k++) rowResults[k] = false; for(int k=0;k<elements.size();k++){ for(int l=0; l<elements.get(k).size();l++){ boolean elementValue = possibilities[new Integer(elements.get(k).get(l))-1][j]; if(!elementsSign.get(k).get(l)){ elementValue = !elementValue; } if(elementValue){ rowResults[k] = true; break; } } } boolean result = true; for(int k=0;k<elements.size();k++){ result = result & rowResults[k]; } if(result) numberOfSuccesses = numberOfSuccesses + 1; } System.out.println(numberOfSuccesses); } private static String[] divideRows(String inputText){ String[] dividedText = inputText.split("\\^"); return dividedText; } private static String[] divideElements(String inputText){ String[] elements = inputText.split("v"); return elements; } private static String prepareElement(String inputElement){ String preparedElement = inputElement.trim(); preparedElement = preparedElement.replace("(", ""); preparedElement = preparedElement.replace(")", ""); preparedElement = preparedElement.replace("x", ""); return preparedElement; } private class ListString{ private String[] table; public ListString(){ table = new String[0]; } public void add(String elementToAdd){ String[] table2 = new String[table.length+1]; table2 = write(table2); table2[table2.length-1] = elementToAdd; table = table2; } public int size(){ return table.length; } public String get(int index){ return table[index]; } private String[] write(String[] toWrite){ for(int i=0;i<table.length;i++) toWrite[i]=table[i]; return toWrite; } } private class ListListString{ private ListString[] table; public ListListString(){ table = new ListString[0]; } public void add(ListString elementToAdd){ ListString[] table2 = new ListString[table.length+1]; table2 = write(table2); table2[table2.length-1] = elementToAdd; table = table2; } public int size(){ return table.length; } public ListString get(int index){ return table[index]; } private ListString[] write(ListString[] toWrite){ for(int i=0;i<table.length;i++) toWrite[i]=table[i]; return toWrite; } } private class ListBoolean{ private Boolean[] table; public ListBoolean(){ table = new Boolean[0]; } public void add(Boolean elementToAdd){ Boolean[] table2 = new Boolean[table.length+1]; table2 = write(table2); table2[table2.length-1] = elementToAdd; table = table2; } public int size(){ return table.length; } public Boolean get(int index){ return table[index]; } private Boolean[] write(Boolean[] toWrite){ for(int i=0;i<table.length;i++) toWrite[i]=table[i]; return toWrite; } } private class ListListBoolean{ private ListBoolean[] table; public ListListBoolean(){ table = new ListBoolean[0]; } public void add(ListBoolean elementToAdd){ ListBoolean[] table2 = new ListBoolean[table.length+1]; table2 = write(table2); table2[table2.length-1] = elementToAdd; table = table2; } public ListBoolean get(int index){ return table[index]; } private ListBoolean[] write(ListBoolean[] toWrite){ for(int i=0;i<table.length;i++) toWrite[i]=table[i]; return toWrite; } } }
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class cnf { public static void main(String[] args) { cnf element = new cnf(); element.doWork(args); System.exit(0); } public void doWork(String[] args){ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Integer i = null; String inputText = null; try { i = new Integer(br.readLine()); inputText = br.readLine(); } catch (IOException e) { e.printStackTrace(); } int numberOfSuccesses = 0; ListListString elements = new ListListString(); ListListBoolean elementsSign = new ListListBoolean(); boolean[][] possibilities = new boolean[i][(int) Math.pow(2, i)]; ListBoolean pattern; ListBoolean pattern2; for(int indexPreparePossibilities=0; indexPreparePossibilities<possibilities.length;indexPreparePossibilities++){ pattern = new ListBoolean(); for(int indexPattern1=0; indexPattern1<Math.pow(2, indexPreparePossibilities);indexPattern1++) pattern.add(true); for(int indexPattern2=0; indexPattern2<Math.pow(2, indexPreparePossibilities);indexPattern2++) pattern.add(false); pattern2 = new ListBoolean(); for(int indexAntyPattern1=pattern.size(); indexAntyPattern1>0;indexAntyPattern1--) pattern2.add(pattern.get(indexAntyPattern1-1)); for(int j=0;j<possibilities[indexPreparePossibilities].length; j = j + pattern.size()*2){ for(int k = 0; k<pattern.size(); k++){ possibilities[indexPreparePossibilities][j+k]=pattern.get(k); } for(int k = 0; k<pattern.size(); k++){ if(j+pattern.size()+k < possibilities[indexPreparePossibilities].length) possibilities[indexPreparePossibilities][j+pattern.size()+k]=pattern2.get(k); else break; } } } String[] dividedText = divideRows(inputText); for(String text: dividedText){ ListString row = new ListString(); ListBoolean rowSign = new ListBoolean(); String[] textElements = divideElements(text); for(String elementToAdd : textElements){ String preparedElement = prepareElement(elementToAdd); if(preparedElement.startsWith("~")){ rowSign.add(false); row.add(preparedElement.replace("~", "")); } else{ rowSign.add(true); row.add(preparedElement); } } elements.add(row); elementsSign.add(rowSign); } for(int j=0;j<possibilities[0].length;j++){ boolean[] rowResults = new boolean[elements.size()]; for(int k=0;k<elements.size();k++) rowResults[k] = false; for(int k=0;k<elements.size();k++){ for(int l=0; l<elements.get(k).size();l++){ boolean elementValue = possibilities[new Integer(elements.get(k).get(l))-1][j]; if(!elementsSign.get(k).get(l)){ elementValue = !elementValue; } if(elementValue){ rowResults[k] = true; break; } } } boolean result = true; for(int k=0;k<elements.size();k++){ result = result & rowResults[k]; } if(result) numberOfSuccesses = numberOfSuccesses + 1; } System.out.println(numberOfSuccesses); } private static String[] divideRows(String inputText){ String[] dividedText = inputText.split("\\^"); return dividedText; } private static String[] divideElements(String inputText){ String[] elements = inputText.split("v"); return elements; } private static String prepareElement(String inputElement){ String preparedElement = inputElement.trim(); preparedElement = preparedElement.replace("(", ""); preparedElement = preparedElement.replace(")", ""); preparedElement = preparedElement.replace("x", ""); return preparedElement; } private class ListString{ private String[] table; public ListString(){ table = new String[0]; } public void add(String elementToAdd){ String[] table2 = new String[table.length+1]; table2 = write(table2); table2[table2.length-1] = elementToAdd; table = table2; } public int size(){ return table.length; } public String get(int index){ return table[index]; } private String[] write(String[] toWrite){ for(int i=0;i<table.length;i++) toWrite[i]=table[i]; return toWrite; } } private class ListListString{ private ListString[] table; public ListListString(){ table = new ListString[0]; } public void add(ListString elementToAdd){ ListString[] table2 = new ListString[table.length+1]; table2 = write(table2); table2[table2.length-1] = elementToAdd; table = table2; } public int size(){ return table.length; } public ListString get(int index){ return table[index]; } private ListString[] write(ListString[] toWrite){ for(int i=0;i<table.length;i++) toWrite[i]=table[i]; return toWrite; } } private class ListBoolean{ private Boolean[] table; public ListBoolean(){ table = new Boolean[0]; } public void add(Boolean elementToAdd){ Boolean[] table2 = new Boolean[table.length+1]; table2 = write(table2); table2[table2.length-1] = elementToAdd; table = table2; } public int size(){ return table.length; } public Boolean get(int index){ return table[index]; } private Boolean[] write(Boolean[] toWrite){ for(int i=0;i<table.length;i++) toWrite[i]=table[i]; return toWrite; } } private class ListListBoolean{ private ListBoolean[] table; public ListListBoolean(){ table = new ListBoolean[0]; } public void add(ListBoolean elementToAdd){ ListBoolean[] table2 = new ListBoolean[table.length+1]; table2 = write(table2); table2[table2.length-1] = elementToAdd; table = table2; } public ListBoolean get(int index){ return table[index]; } private ListBoolean[] write(ListBoolean[] toWrite){ for(int i=0;i<table.length;i++) toWrite[i]=table[i]; return toWrite; } } } |