import java.util.*; import java.util.zip.*; import java.util.List; import java.util.regex.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; import java.util.concurrent.locks.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; import javax.swing.table.*; import java.io.*; import java.net.*; import java.lang.reflect.*; import java.lang.ref.*; import java.lang.management.*; import java.security.*; import java.security.spec.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import java.math.*; public class main { public static void main(String[] args) throws Exception { assertEquals(1, ocr_glyphCount("{symbol}")); assertEquals(2, ocr_glyphCount("ab")); assertEquals(1, ocr_glyphCount("[ab]")); assertEquals(2, ocr_glyphCount("[ab] c")); assertEquals("[a]", ocr_unescape("\\[a\\]")); } static A assertEquals(Object x, A y) { return assertEquals(null, x, y); } static A assertEquals(String msg, Object x, A y) { if (!(x == null ? y == null : x.equals(y))) throw fail((msg != null ? msg + ": " : "") + x + " != " + y); return y; } static int ocr_glyphCount(String s) { return l(ocr_parseGlyphs(dropSpaces(s))); } static String ocr_unescape(String s) { StringBuilder buf = new StringBuilder(); for (int i = 0; i < l(s); i++) { if (s.charAt(i) == '\\' && i+1 < l(s)) { // parse escaped character ++i; } buf.append(s.charAt(i)); } return str(buf); } static String str(Object o) { return String.valueOf(o); } static RuntimeException fail() { throw new RuntimeException("fail"); } static RuntimeException fail(Object msg) { throw new RuntimeException(String.valueOf(msg)); } static RuntimeException fail(String msg) { throw new RuntimeException(unnull(msg)); } static RuntimeException fail(String msg, Throwable innerException) { throw new RuntimeException(msg, innerException); } // disabled for now to shorten some programs /*static RuntimeException fail(S msg, O... args) { throw new RuntimeException(format(msg, args)); }*/ static List ocr_parseGlyphs(String s) { List l = new ArrayList(); for (int i = 0; i < l(s); i++) if (s.charAt(i) == '\\' && i+1 < l(s)) { // parse escaped character l.add(substring(s, i, i+2)); ++i; } else if (s.charAt(i) == '[') { // parse group int j = i+1; StringBuilder buf = new StringBuilder(); while (j < l(s) && s.charAt(j) != ']') { if (s.charAt(j) == '\\' && j+1 < l(s)) buf.append(s.charAt(j++)); buf.append(s.charAt(j++)); } l.add(str(buf)); i = j; } else if (s.charAt(i) == '{') { // parse symbol int j = i; StringBuilder buf = new StringBuilder(); while (j < l(s) && s.charAt(j) != '}') { if (s.charAt(j) == '\\' && j+1 < l(s)) buf.append(s.charAt(j++)); buf.append(s.charAt(j++)); } buf.append(s.charAt(j)); l.add(str(buf)); i = j; } else l.add(substring(s, i, i+1)); return l; } static int l(Object[] a) { return a == null ? 0 : a.length; } static int l(boolean[] a) { return a == null ? 0 : a.length; } static int l(byte[] a) { return a == null ? 0 : a.length; } static int l(int[] a) { return a == null ? 0 : a.length; } static int l(float[] a) { return a == null ? 0 : a.length; } static int l(char[] a) { return a == null ? 0 : a.length; } static int l(Collection c) { return c == null ? 0 : c.size(); } static int l(Map m) { return m == null ? 0 : m.size(); } static int l(CharSequence s) { return s == null ? 0 : s.length(); } static int l(Object o) { return o instanceof String ? l((String) o) : l((Collection) o); // incomplete } static String dropSpaces(String s) { return unnull(s).replace(" ", ""); } static String unnull(String s) { return s == null ? "" : s; } static List unnull(List l) { return l == null ? emptyList() : l; } static Iterable unnull(Iterable i) { return i == null ? emptyList() : i; } static Object[] unnull(Object[] a) { return a == null ? new Object[0] : a; } static BitSet unnull(BitSet b) { return b == null ? new BitSet() : b; } static String substring(String s, int x) { return safeSubstring(s, x); } static String substring(String s, int x, int y) { return safeSubstring(s, x, y); } // hopefully covers all cases :) static String safeSubstring(String s, int x, int y) { if (s == null) return null; if (x < 0) x = 0; if (x > s.length()) return ""; if (y < x) y = x; if (y > s.length()) y = s.length(); return s.substring(x, y); } static String safeSubstring(String s, int x) { return safeSubstring(s, x, l(s)); } static List emptyList() { return new ArrayList(); //ret Collections.emptyList(); } }