/** * If the outbound level is BINARY, convert the string field to binary, then pad to the left with * the appropriate number of zero bits to reach a number of bits specified by the bitLength * attribute of the TDT definition file. */ private void binaryPadding(Map<String, String> extraparams, Field tdtfield) { String fieldname = tdtfield.getName(); int reqbitlength = tdtfield.getBitLength(); String value; String binaryValue = fieldToBinary(tdtfield, extraparams); if (binaryValue.length() < reqbitlength) { int extraBitLength = reqbitlength - binaryValue.length(); StringBuilder zeroPaddedBinaryValue = new StringBuilder(""); for (int i = 0; i < extraBitLength; i++) { zeroPaddedBinaryValue.append("0"); } zeroPaddedBinaryValue.append(binaryValue); value = zeroPaddedBinaryValue.toString(); } else { if (binaryValue.length() > reqbitlength) throw new TDTException( "Binary value [" + binaryValue + "] for field " + fieldname + " exceeds maximum allowed " + reqbitlength + " bits. Decimal value was " + extraparams.get(fieldname)); value = binaryValue; } extraparams.put(fieldname, value); }
private static String convert(List<List<Particle>> lists) { StringBuilder sb = new StringBuilder(); for (List<Particle> list : lists) { if (list.isEmpty()) sb.append('.'); else sb.append('X'); } return sb.toString(); }
public int totalNQueens(int n) { StringBuilder[] board = new StringBuilder[n]; for (int i = 0; i < n; i++) { board[i] = new StringBuilder(); for (int j = 0; j < n; j++) { board[i].append('.'); } } for (int i = 0; i < n / 2; i++) { board[0].setCharAt(i, 'Q'); dfs(n, 1, board); board[0].setCharAt(i, '.'); } ArrayList<String[]> aux = new ArrayList<String[]>(); for (String[] k : res) { String[] tmp = new String[n]; for (int i = 0; i < n; i++) { StringBuilder sb = new StringBuilder(k[i]).reverse(); tmp[i] = sb.toString(); } aux.add(tmp); } res.addAll(aux); if (n % 2 != 0) { board[0].setCharAt(n / 2, 'Q'); dfs(n, 1, board); board[0].setCharAt(n / 2, '.'); } return res.size(); }
/** * Return a set of the variables in the supplied expression. Note: Substitutions which are in the * constant table are not included. */ public Set<String> getVariablesWithin(String exp) { Set<String> all = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); String add = null; if (separators == null) { StringBuilder sep = new StringBuilder(10); for (char chr = 0; chr < operators.length; chr++) { if (operators[chr] != null && !operators[chr].internal) { sep.append(chr); } } sep.append("()"); separators = sep.toString(); } for (StringTokenizer tkz = new StringTokenizer(exp, separators, true); tkz.hasMoreTokens(); ) { String tkn = tkz.nextToken().trim(); if (tkn.length() != 0 && Character.isLetter(tkn.charAt(0))) { add = tkn; } else if (tkn.length() == 1 && tkn.charAt(0) == '(') { add = null; } else if (add != null && !constants.containsKey(add)) { all.add(add); } } if (add != null && !constants.containsKey(add)) { all.add(add); } return all; }
public static String set(final String lhs, BigInteger index, char value) { int idx = index.intValue(); // hmmm, not exactly efficient! StringBuilder sb = new StringBuilder(lhs); sb.setCharAt(idx, value); return sb.toString(); }
public void run() { // Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(); StringBuilder sb = new StringBuilder(1000000); // System.setOut(new PrintStream(new BufferedOutputStream(System.out))); final int N = sc.nextInt(); final int M = sc.nextInt(); final int K = (int) floor(sqrt(N)); final int[] A = new int[N + 1]; A[0] = 1; for (int i = 1; i <= N; i++) A[i] = sc.nextInt(); final int[] next = new int[N + 1]; final int[] step = new int[N + 1]; final int[] last = new int[N + 1]; for (int i = N; i > 0; i--) { int j = i + A[i]; if (j > N || j / K > i / K) { last[i] = i; step[i] = 1; next[i] = j; } else { last[i] = last[j]; step[i] = step[j] + 1; next[i] = next[j]; } } for (int t = 0; t < M; t++) if (sc.nextInt() == 1) { int i = sc.nextInt(); int j = 0; int k = 0; while (i <= N) { j += step[i]; k = last[i]; i = next[i]; } sb.append(k).append(' ').append(j).append('\n'); // System.out.println(k + " " + j); } else { int k = sc.nextInt(); int b = k / K * K; A[k] = sc.nextInt(); for (int i = min(b + K - 1, N); i >= b; i--) { int j = i + A[i]; if (j > N || j / K > i / K) { last[i] = i; step[i] = 1; next[i] = j; } else { last[i] = last[j]; step[i] = step[j] + 1; next[i] = next[j]; } } } // System.out.flush(); System.out.print(sb); }
/** * Returns a string representation of all found arguments. * * @param args array with arguments * @return string representation */ static String foundArgs(final Value[] args) { // compose found arguments final StringBuilder sb = new StringBuilder(); for (final Value v : args) { if (sb.length() != 0) sb.append(", "); sb.append(v instanceof Jav ? Util.className(((Jav) v).toJava()) : v.seqType()); } return sb.toString(); }
public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); }
/** Converts a binary string input into a byte string, using 8-bits per character byte */ private String bytestring2bin(String bytestring) { String binary; StringBuilder buffer = new StringBuilder(""); int len = bytestring.length(); byte[] bytes = bytestring.getBytes(); for (int i = 0; i < len; i++) { buffer.append(padBinary(dec2bin(Integer.toString(bytes[i])), 8)); } binary = buffer.toString(); return binary; }
private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') { buf.appendCodePoint(c); } c = read(); } return buf.toString(); }
/** * Converts an alphanumeric string input into a binary string, using 6-bit compaction of each * ASCII byte */ private String alphanumsix2bin(String alphanumsix) { String binary; StringBuilder buffer = new StringBuilder(""); int len = alphanumsix.length(); byte[] bytes = alphanumsix.getBytes(); for (int i = 0; i < len; i++) { buffer.append(padBinary(dec2bin(Integer.toString(bytes[i] % 64)), 8).substring(2, 8)); } binary = buffer.toString(); return binary; }
/** * Converts a binary string input into a character string output, assuming that 5-bit compaction * was used */ private String bin2uppercasefive(String binary) { String uppercasefive; StringBuilder buffer = new StringBuilder(""); int len = binary.length(); for (int i = 0; i < len; i += 5) { int j = Integer.parseInt(bin2dec(padBinary(binary.substring(i, i + 5), 8))); buffer.append((char) (j + 64)); } uppercasefive = buffer.toString(); return uppercasefive; }
/** * Converts an upper case character string input into a binary string, using 5-bit compaction of * each ASCII byte */ private String uppercasefive2bin(String uppercasefive) { String binary; StringBuilder buffer = new StringBuilder(""); int len = uppercasefive.length(); byte[] bytes = uppercasefive.getBytes(); for (int i = 0; i < len; i++) { buffer.append(padBinary(dec2bin(Integer.toString(bytes[i] % 32)), 8).substring(3, 8)); } binary = buffer.toString(); return binary; }
/** * Converts a binary string input into an ASCII string output, assuming that 7-bit compaction was * used */ private String bin2asciiseven(String binary) { String asciiseven; StringBuilder buffer = new StringBuilder(""); int len = binary.length(); for (int i = 0; i < len; i += 7) { int j = Integer.parseInt(bin2dec(padBinary(binary.substring(i, i + 7), 8))); buffer.append((char) j); } asciiseven = buffer.toString(); return asciiseven; }
/** * Converts an ASCII string input into a binary string, using 7-bit compaction of each ASCII byte */ private String asciiseven2bin(String asciiseven) { String binary; StringBuilder buffer = new StringBuilder(""); int len = asciiseven.length(); byte[] bytes = asciiseven.getBytes(); for (int i = 0; i < len; i++) { buffer.append(padBinary(dec2bin(Integer.toString(bytes[i] % 128)), 8).substring(1, 8)); } binary = buffer.toString(); return binary; }
/** Converts a byte string input into a binary string, using 8-bits per character byte */ private String bin2bytestring(String binary) { String bytestring; StringBuilder buffer = new StringBuilder(""); int len = binary.length(); for (int i = 0; i < len; i += 8) { int j = Integer.parseInt(bin2dec(padBinary(binary.substring(i, i + 8), 8))); buffer.append((char) j); } bytestring = buffer.toString(); return bytestring; }
/** * Make a string from the contents of this JSONArray. The <code>separator</code> string is * inserted between each element. Warning: This method assumes that the data structure is * acyclical. * * @param separator A string that will be inserted between the elements. * @return a string. * @throws JSONException If the array contains an invalid number. */ public String join(String separator) throws JSONException { int len = this.length(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i += 1) { if (i > 0) { sb.append(separator); } sb.append(JSONObject.valueToString(this.myArrayList.get(i))); } return sb.toString(); }
/** * Pads a binary value supplied as a string first parameter to the left with leading zeros in * order to reach a required number of bits, as expressed by the second parameter, reqlen. Returns * a string value corresponding to the binary value left padded to the required number of bits. */ private String padBinary(String binary, int reqlen) { String rv; int l = binary.length(); int pad = (reqlen - (l % reqlen)) % reqlen; StringBuilder buffer = new StringBuilder(""); for (int i = 0; i < pad; i++) { buffer.append("0"); } buffer.append(binary); rv = buffer.toString(); return rv; }
/** * Returns a string built using a particular grammar. Single-quotes strings are counted as literal * strings, whereas all other strings appearing in the grammar require substitution with the * corresponding value from the extraparams hashmap. */ private String buildGrammar(String grammar, Map<String, String> extraparams) { StringBuilder outboundstring = new StringBuilder(); String[] fields = Pattern.compile("\\s+").split(grammar); for (int i = 0; i < fields.length; i++) { if (fields[i].substring(0, 1).equals("'")) { outboundstring.append(fields[i].substring(1, fields[i].length() - 1)); } else { outboundstring.append(extraparams.get(fields[i])); } } return outboundstring.toString(); }
public static void main(String args[]) { int NUM_LETTERS = 27; char[] alfabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '?' }; Scanner in = new Scanner(System.in); int L = in.nextInt(); in.nextLine(); int H = in.nextInt(); in.nextLine(); // to store the letter in asci artM char[][] art = new char[H][L * NUM_LETTERS]; String T = in.nextLine(); // text to translate System.err.println("create array L:" + L + "/H:" + H + "/to translate:" + T); // saving asci art for (int i = 0; i < H; i++) { String ROW = in.nextLine(); // first line of all letters in asci art // the ROW must be saved in L chars groups // System.err.println("readed->" + ROW); char[] aux = ROW.toCharArray(); for (int j = 0; j < aux.length; j++) { // System.err.println("guardando->["+i+"]["+j+"]" + aux[j]); art[i][j] = aux[j]; } } StringBuilder builder = new StringBuilder(); char[] translation = T.toCharArray(); for (int m = 0; m < H; m++) { StringBuilder linea = new StringBuilder(); for (int j = 0; j < translation.length; j++) { // neet to get char -> letter number to get position int position = returnPosition(Character.toUpperCase(translation[j]), alfabet); int start = (position * L); for (int u = start; u < (start + L); u++) { linea.append(art[m][u]); } } System.out.println(linea); } // Write an action using System.out.println() // To debug: System.err.println("Debug messages..."); }
/** * Converts a binary string input into a character string output, assuming that 6-bit compaction * was used */ private String bin2alphanumsix(String binary) { String alphanumsix; StringBuilder buffer = new StringBuilder(""); int len = binary.length(); for (int i = 0; i < len; i += 6) { int j = Integer.parseInt(bin2dec(padBinary(binary.substring(i, i + 6), 8))); if (j < 32) { j += 64; } buffer.append((char) j); } alphanumsix = buffer.toString(); return alphanumsix; }
// Find and unescape a string. private String readString() { StringBuilder sb = new StringBuilder(); char c = next(); if (c != '"') throw new RuntimeException("expecting start of string"); while (true) switch (c = next()) { case '"': return sb.toString(); case '\\': sb.append(readEscaped()); continue; default: if (Character.isISOControl(c)) throw new RuntimeException("illegal character in string"); sb.append(c); } }
public String readword() throws IOException { StringBuilder b = new StringBuilder(); int c; c = in.read(); while (c >= 0 && c <= ' ') { c = in.read(); } if (c < 0) { return ""; } while (c > ' ') { b.append((char) c); c = in.read(); } return b.toString(); }
void solve() { int n = nextInt(); HashMap<Long, Integer>[] hm = new HashMap[33]; for (int i = 0; i < hm.length; i++) { hm[i] = new HashMap<Long, Integer>(); } boolean[] allows = new boolean[n]; for (int i = 0; i < n; i++) { String s = nextToken(); nextToken(); String ip = nextToken(); if (ip.indexOf("/") == -1) { ip = ip + "/32"; } allows[i] = s.equals("allow"); int p = ip.indexOf("/"); String s1 = ip.substring(0, p); String s2 = ip.substring(p + 1); int e = Integer.parseInt(s2); long r = ipToInteger(s1); r &= ~((1L << (32 - e)) - 1); if (!hm[e].containsKey(r)) hm[e].put(r, i); // System.err.println("Query " + i + " = " + r); } int m = nextInt(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < m; i++) { String ip = nextToken(); long r = ipToInteger(ip); int min = Integer.MAX_VALUE; // System.err.println("Cur = " + i); for (int j = 0; j <= 32; j++) { long e = r; e &= ~((1L << (32 - j)) - 1); // System.err.println(e + " " + j); if (hm[j].containsKey(e)) { min = Math.min(min, hm[j].get(e)); } } if (min == Integer.MAX_VALUE || allows[min]) { sb.append("A"); } else { sb.append("D"); } } out.println(sb); }
public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(new BufferedInputStream(System.in)); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int num; while ((num = scanner.nextInt()) != 0) { ArrayList<String> all = new ArrayList<String>(); for (int i = 0; i < num; i++) { all.add(scanner.next()); } Collections.sort(all, comptr); StringBuilder sb = new StringBuilder(""); for (String s : all) { sb.append(s); } System.out.println(sb); } }
private Number readNumber() { char c; boolean d = false; StringBuilder sb = new StringBuilder(); while (true) switch (c = peek()) { case '.': case 'e': case 'E': case '+': d = true; case '-': sb.append(next()); continue; default: if (check(c, '0', '9')) sb.append(next()); else if (check(c, ",:; \n\r\t>}])")) { return d ? new BigDecimal(sb.toString()) : new BigInteger(sb.toString()); } else throw new RuntimeException("unexpected character: " + c); } }
public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.next(); int l = s.length(); Double sqrl = Math.sqrt(l); Double low = Math.floor(sqrl); Double high = Math.ceil(sqrl); int r = 0; int c = 0; if (low == high) { r = low.intValue(); c = low.intValue(); } else if (low * high >= l) { r = low.intValue(); c = high.intValue(); } else { r = high.intValue(); c = high.intValue(); } // System.out.println("Row: " + r); // System.out.println("Col: " + c); StringBuilder sb = new StringBuilder(); char en[] = s.toCharArray(); for (int i = 0; i < c; i++) { for (int j = 0; j < r; j++) { if (j * c + i < l) { sb.append(en[j * c + i]); } } sb.append(" "); } System.out.println(sb.toString()); }
/** pad a value according the field definition. */ private void padField(Map<String, String> extraparams, Field field) { String name = field.getName(); String value = extraparams.get(name); PadDirectionList padDir = field.getPadDir(); int requiredLength = field.getLength(); // assert value != null; if (value == null) return; String padCharString = field.getPadChar(); // if no pad char specified, don't attempt padding if (padCharString == null) return; assert padCharString.length() > 0; char padChar = padCharString.charAt(0); StringBuilder buf = new StringBuilder(requiredLength); if (padDir == PadDirectionList.LEFT) { for (int i = 0; i < requiredLength - value.length(); i++) buf.append(padChar); buf.append(value); } else if (padDir == PadDirectionList.RIGHT) { buf.append(value); for (int i = 0; i < requiredLength - value.length(); i++) buf.append(padChar); } assert buf.length() == requiredLength; if (requiredLength != value.length()) { // System.out.println(" updated " + name + " to '" + buf + "'"); extraparams.put(name, buf.toString()); } /* else { StringBuilder mybuf = new StringBuilder(); for (int i = 0; i < value.length(); i++) { if (i > 0) mybuf.append(','); mybuf.append('\''); mybuf.append(value.charAt(i)); mybuf.append('\''); } System.out.println(" field " + name + " not padded as " + mybuf.toString() + " is already " + requiredLength + " characters long"); } */ }
// Try to read an atom. private Atom readAtom() { StringBuilder sb = new StringBuilder(); for (char c = peek(); validAtomPart(c); c = peek()) sb.append(next()); return new Atom(sb.toString()); }
/* ------------------------------------------------------------------------------------------------------- This method: This method: -- Takes in three paramters: 1. array - this is the byte array that actually holds the document contents 2. md5Hases - holds the entire hash values of the document 3. Divisor1/Divisor2/divisor3... - main and back up divisors 5. The remainder we are looking for 6/7. min/max boundaries -- We will start running the karb rabin algorithm -- We will find the boundaries using mod values and once they equal the mod value we have stored -- we also have the divsor2/3 .. which are backup divisors. If we don't find a boundary by the divisor1 once we hit the maxBoundary -- we will see if we have one with divisor2, if not, then we will see if we have one with divisor3 and so on -- We will hash everything in that hash boundary and store it -------------------------------------------------------------------------------------------------------- */ private static void runTddd( byte[] array, ArrayList<Long> md5Hashes, long divisor1, long divisor2, long divisor3, long remainder, long minBoundary, long maxBoundary) { int documentStart = 0; // used to keep track of where the boundaries are boolean match = false; // used to ck if we encountered a match int backUpBreakPoint = -1; // used to store the backup breakpoint int secondBackUpBreakPoint = -1; // used with the divisor3 StringBuilder builder = new StringBuilder(); int i = documentStart + (int) minBoundary - 1; // so we start at the minimum // loop through all the values in the document for (; i < md5Hashes.size() - 1; ++i) { // if ((i - documentStart + 1) < minBoundary) // if the size of this boundary is less than // the min, continue looping // continue; /*----------------------------------------------------------------- - If the mod of this equals the modvalue we defined, then - this is a boundary ------------------------------------------------------------------*/ if ((md5Hashes.get(i - 1) + md5Hashes.get(i) + md5Hashes.get(i + 1)) % divisor1 == remainder) // ck if this equals the mod value { // Hash all the values in the range (documentStart,current(i)) // Remember we only want to hash the original VALUES from the array that contains the // original // content of the file. Not the hash values in the md5Hash Array for (int j = documentStart; j <= i; ++j) { builder.append(array[j]); // store everything upto the current value } String original = builder.toString(); // if the string is a perfect match ( hash and original string) if (HashClass.is_string_match(original, table)) // iinsert the hash in the table) coverage += i - documentStart + 1; // this is the amount of bytes we saved documentStart = i + 1; // set this as the beginning of the new boundary backUpBreakPoint = -1; // reset this secondBackUpBreakPoint = -1; numOfPieces++; // increment the num of pieces i = i + (int) minBoundary - 1; // skip all the way here builder.setLength(0); // reset the stringBuilder for the next round } else if ((md5Hashes.get(i - 1) + md5Hashes.get(i) + md5Hashes.get(i + 1)) % divisor2 == remainder) { // check if this is the backup point backUpBreakPoint = i; // this is the backup breakpoint } else if ((md5Hashes.get(i - 1) + md5Hashes.get(i) + md5Hashes.get(i + 1)) % divisor2 == remainder) { secondBackUpBreakPoint = i; // set the second backup point } if ((i - documentStart + 1) >= maxBoundary) { // we have reached the maximum // ck if we have a backUpbreakpoint int point; if (backUpBreakPoint != -1) // if we do, set this as the boundary point = backUpBreakPoint; else if (secondBackUpBreakPoint != -1) { point = secondBackUpBreakPoint; // if we don't have a first break point, find the second } else point = i; // else this current value of i is the breakpoint // Hash all the values in the range (documentStart,current(i)) // Remember we only want to hash the original VALUES from the array that contains the // original // content of the file. Not the hash values in the md5Hash Array for (int j = documentStart; j <= point; ++j) { builder.append(array[j]); // store everything upto the current value } String original = builder.toString(); if (HashClass.is_string_match(original, table)) coverage += point - documentStart + 1; // this is the amount of bytes we saved numOfPieces++; // increment the num of pieces documentStart = point + 1; // set this as the beginning of the new boundary backUpBreakPoint = -1; // reset this secondBackUpBreakPoint = -1; // reset the secondBackUp point i = point + (int) minBoundary - 1; // skip all the way here ; builder.setLength(0); // reset the stringBuilder for the next round } } // end of the for loop // ------------------------------------------------------------------------------------------- // we are missing the last boundary, so hash that last value // We will also check against our values of the strings we already have, and if we encountered // this // already, then we will simply increment the counter, otherwise we will insert it in the // hashtable // and increase our miss counter // ---------------------------------------------------------------------------------------------- for (int j = documentStart; j < array.length; ++j) { builder.append(array[j]); } // only compute hash and insert into our hashtable only if the string buffer isn't empty String original = builder.toString(); if (HashClass.is_string_match(original, table)) coverage += array.length - documentStart; // this is the amount of bytes we saved numOfPieces++; // increment the num of pieces } // end of the method