public int minimumMoves(String[] boardString) {
   n = boardString.length;
   m = boardString[0].length();
   board = new int[n];
   for (int i = 0; i < n; ++i) {
     for (int j = 0; j < m; ++j) {
       if (boardString[i].charAt(j) == 'W') {
         board[i] |= 1 << j;
       }
     }
   }
   int answer = Integer.MAX_VALUE;
   all = (1 << m) - 1;
   for (int type = 0; type < 1 << m; ++type) {
     answer = Math.min(answer, solve(type, 0));
     for (int first = type; first > 0; first = (first - 1) & type) {
       answer = Math.min(answer, solve(type, first));
     }
     answer = Math.min(answer, solve(type, all ^ type));
     for (int first = all ^ type; first > 0; first = (first - 1) & (all ^ type)) {
       answer = Math.min(answer, solve(type, first));
     }
   }
   return answer == Integer.MAX_VALUE ? -1 : answer;
 }
  public static void main(String[] args) throws java.lang.Exception {
    // let_me_start

    String s = s();
    String t = s();
    int c[] = new int[150];
    int d[] = new int[150];
    String S = s.toUpperCase();
    String T = t.toUpperCase();
    int C[] = new int[150];
    int D[] = new int[150];

    int n = s.length();
    int m = t.length();

    // int arr[] = is((int)n);

    long ans = 0;
    for (int i = 1; i <= n; i++) {
      c[s.charAt(i - 1)]++;
    }

    for (int i = 1; i <= m; i++) {
      d[t.charAt(i - 1)]++;
    }
    for (int i = 1; i <= n; i++) {
      C[S.charAt(i - 1)]++;
    }

    for (int i = 1; i <= m; i++) {
      D[T.charAt(i - 1)]++;
    }

    int b = 0;
    int min = 1000000000;
    for (int i = 1; i <= 149; i++) {
      b += Math.min(c[i], d[i]);
      d[i] -= Math.min(c[i], d[i]);
    }
    int a = 0;
    min = 1000000000;
    for (int i = 1; i <= 149; i++) {
      a += Math.min(C[i], D[i]);
      D[i] -= Math.min(C[i], D[i]);
    }
    a = a - b;
    out.write("" + b + " " + a + "\n");
    out.flush();
    return;
  }
Example #3
0
  public static int compare(Record o1, Record o2) {
    ArrayList<String> mKeys = new ArrayList<String>(o1.keySet());
    ArrayList<String> tKeys = new ArrayList<String>(o2.keySet());
    Collections.sort(mKeys);
    Collections.sort(tKeys);

    for (int i = 0; i != Math.min(mKeys.size(), tKeys.size()); ++i) {
      String mk = mKeys.get(i);
      String tk = tKeys.get(i);
      int c = mk.compareTo(tk);
      if (c != 0) {
        return c;
      }
      String mv = o1.get(mk).toString();
      String tv = o2.get(tk).toString();
      c = mv.compareTo(tv);
      if (c != 0) {
        return c;
      }
    }

    if (mKeys.size() < tKeys.size()) {
      return -1;
    } else if (mKeys.size() > tKeys.size()) {
      return 1;
    } else {
      return 0;
    }
  }
Example #4
0
 public int count(
     String[] s1000,
     String[] s100,
     String[] s10,
     String[] s1,
     String[] t1000,
     String[] t100,
     String[] t10,
     String[] t1) {
   String S1000 = concate(s1000);
   String S100 = concate(s100);
   String S10 = concate(s10);
   String S1 = concate(s1);
   String T1000 = concate(t1000);
   String T100 = concate(t100);
   String T10 = concate(t10);
   String T1 = concate(t1);
   int n = S1000.length();
   Interval[] intervals = new Interval[n];
   for (int i = 0; i < n; ++i) {
     intervals[i] =
         new Interval(
             Integer.parseInt(
                 S1000.substring(i, i + 1) + S100.charAt(i) + S10.charAt(i) + S1.charAt(i)),
             Integer.parseInt(
                 T1000.substring(i, i + 1) + T100.charAt(i) + T10.charAt(i) + T1.charAt(i)));
   }
   Arrays.sort(intervals);
   int leftmost = Integer.MAX_VALUE;
   int rightmost = Integer.MIN_VALUE;
   for (int i = 0; i < n; ++i) {
     leftmost = Math.min(leftmost, intervals[i].b);
     rightmost = Math.max(rightmost, intervals[i].a);
   }
   int answer = 0;
   for (int i = 0; i < n; ++i) {
     int total = 0;
     int now = leftmost;
     int j = 0;
     while (true) {
       if (intervals[i].a <= now) {
         now = Math.max(now, intervals[i].b);
       }
       if (now >= rightmost) {
         break;
       }
       int best = now;
       while (j < n && intervals[j].a <= now) {
         best = Math.max(best, intervals[j++].b);
       }
       if (best <= now) {
         return -1;
       }
       total++;
       now = best;
     }
     answer += total;
   }
   return answer;
 }
  // Constructor::TextContext
  //
  // Generates a TextContext Object.
  //
  // Generates before and after contexts based on given length and values
  //
  // Parameters
  //
  // * primary -- TextPrimary object to be tested against context
  // * constraint -- TextConstraint object attached to this context
  // * checkSumType -- Hash for checksum
  // * contextLength - length (in chars) of the context used for testing
  //
  public TextContext(
      TextPrimary primary, TextConstraint constraint, HashType checkSumType, int contextLength) {
    super();
    this.checkSumType = checkSumType;
    //	Testing if content matches the bit-checksum tests

    this.checkSum = checkSum(primary.getContent(), checkSumType);

    int beforeStart = constraint.getStartPos() - contextLength;
    beforeStart = Math.max(0, beforeStart);
    int beforeEnd = constraint.getStartPos();

    int afterStart = constraint.getEndPos();
    int afterEnd = constraint.getEndPos() + contextLength;
    afterEnd = Math.min(primary.getContent().length(), afterEnd);

    // Evaluating how much of selected text to store
    this.totalSelectionLength = primary.getContent().length();
    int cLength = this.totalSelectionLength;
    if (this.totalSelectionLength > DEFAULT_CONTEXTLENGTH) {
      double half = (double) (this.totalSelectionLength / 2);
      cLength = (int) (Math.floor(half * percentStorage));
      this.beginSel = primary.getContent().substring(beforeEnd, (beforeEnd + cLength));
      this.endSel = primary.getContent().substring((afterStart - cLength), afterStart);
      this.totalSelection = this.beginSel.concat(this.endSel);
    } else {
      // Use the entire selection
      this.beginSel = "";
      this.endSel = "";
      this.totalSelection = primary.getContent();
    }

    this.beforeContext = primary.getContent().substring(beforeStart, beforeEnd);
    this.afterContext = primary.getContent().substring(afterStart, afterEnd);
  }
Example #6
0
 public static void main(String[] args) {
   Scanner in = new Scanner(System.in);
   n = in.nextInt();
   l = new BigInteger(in.next());
   k = in.nextInt();
   m = in.nextInt();
   BigInteger w = BigInteger.TEN.pow(m);
   for (int i = 0; i <= n; ++i) a[i] = new BigInteger(in.next());
   for (int i = 0; i < Math.min(k, n + 1); ++i) {
     t = a[0];
     for (int j = 1; j <= n; ++j) {
       t = t.multiply(l);
       t = t.add(a[j]);
     }
     t = t.mod(w);
     q[n][i] = t.mod(w);
     int ret = 0;
     c = t.toString().toCharArray();
     int ll = c.length;
     for (int j = 0; j < Math.min(m, ll); ++j) {
       ret += (c[ll - 1 - j] - '0') * (c[ll - 1 - j] - '0');
     }
     System.out.println(ret);
     l = l.add(BigInteger.ONE);
   }
   if (k > n) {
     for (int i = n - 1; i >= 0; --i) {
       for (int j = 0; j <= i; j++) q[i][j] = q[i + 1][j + 1].subtract(q[i + 1][j]).mod(w);
     }
     for (int i = 1; i <= n; ++i) q[0][i] = q[0][0];
     int po = 1;
     for (int i = n + 1; i < k; ++i) {
       for (int j = 1; j <= n; ++j)
         q[j][(po + j) % (n + 1)] =
             q[j - 1][(po + j - 1) % (n + 1)].add(q[j][(po + j - 1) % (n + 1)]).mod(w);
       int ret = 0;
       c = q[n][(po + n) % (n + 1)].mod(w).toString().toCharArray();
       int ll = c.length;
       for (int j = 0; j < Math.min(m, ll); ++j) {
         ret += (c[ll - 1 - j] - '0') * (c[ll - 1 - j] - '0');
       }
       System.out.println(ret);
       l = l.add(BigInteger.ONE);
       ++po;
     }
   }
 }
Example #7
0
  /**
   * Adds/subtracts the specified yearMonth duration.
   *
   * @param dur duration
   * @param plus plus/minus flag
   * @param ii input info
   * @throws QueryException query exception
   */
  final void calc(final YMDur dur, final boolean plus, final InputInfo ii) throws QueryException {
    final long m = plus ? dur.mon : -dur.mon;
    final long mn = mon + m;
    mon = (byte) mod(mn, 12);
    yea += div(mn, 12);
    day = (byte) Math.min(dpm(yea, mon) - 1, day);

    if (yea <= MIN_YEAR || yea > MAX_YEAR) throw YEARRANGE_X.get(ii, yea);
  }
  private ShingleCloud createSC(int needleLength, String hayStack) {

    ShingleCloud sc = new ShingleCloud(hayStack);
    sc.setTokenizer(new CharacterTokenizer());

    int nGramSize = Math.min((int) (needleLength), 20);
    sc.setNGramSize(nGramSize);

    sc.setMinimumNumberOfOnesInMatch((int) (1));
    sc.setSortMatchesByRating(true);

    return sc;
  }
Example #9
0
 void perm(long[] r, int index) {
   if (index == r.length) {
     min = Math.min(min, dist(r));
     return;
   }
   for (int i = index; i < r.length; i++) {
     long x = r[index];
     r[index] = r[i];
     r[i] = x;
     perm(r, index + 1);
     x = r[index];
     r[index] = r[i];
     r[i] = x;
   }
 }
 private int[] getWidths(TableModel tableModel) {
   int[] widths = new int[tableModel.getColumnCount()];
   for (int r = 0;
       r < Math.min(tableModel.getRowCount(), 500);
       r++) { // 500 is not for performance, but for using only a sample of data with huge table
     for (int c = 0; c < tableModel.getColumnCount(); c++) {
       Object o = tableModel.getValueAt(r, c);
       if (o instanceof String) {
         String s = ((String) o).trim();
         if (s.length() > widths[c]) widths[c] = s.length();
       }
     }
   }
   return widths;
 }
Example #11
0
 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);
 }
Example #12
0
 void solve() {
   int n = nextInt();
   int m = nextInt();
   int l = 1;
   int r = n;
   for (int i = 0; i < m; i++) {
     nextToken();
     nextToken();
     String s = nextToken();
     nextToken();
     int k = nextInt();
     if (s.equals("left")) {
       r = Math.min(r, k - 1);
     } else {
       l = Math.max(l, k + 1);
     }
   }
   if (l > r) {
     out.println(-1);
   } else {
     out.println(r - l + 1);
   }
 }
 private int clamp(int val, int min, int max) {
   return Math.max(Math.min(val, max), min);
 }
Example #14
0
  public Main() {
    try {
      in = new BufferedReader(new InputStreamReader(System.in));
      // minimum distance from D to K
      int numCities = nextInt();
      int tradeRoutes = nextInt();
      int[][] adjacencyMatrix = new int[numCities][numCities];
      int[] minDistance = new int[numCities];
      Arrays.fill(minDistance, 100000000);
      // Arrays.fill(adjacencyMatrix, -1);

      // int [] pencilCosts = new int[
      Node[] cities = new Node[numCities];
      for (int x = 0; x < tradeRoutes; x++) {
        int cityA = nextInt() - 1;
        int cityB = nextInt() - 1;
        int cost = nextInt();

        if (cities[cityA] == null) cities[cityA] = new Node(cityA);
        if (cities[cityB] == null) cities[cityB] = new Node(cityB);
        adjacencyMatrix[cityA][cityB] = cost;
        adjacencyMatrix[cityB][cityA] = cost;

        // cities[cityA].routes.add(new Edge(cost, cities[cityB]));
        // cities[cityB].routes.add(new Edge(cost, cities[cityA]));
      }

      int numStores = nextInt();
      int[] pencilCosts = new int[numCities];
      Arrays.fill(pencilCosts, -1);
      for (int x = 0; x < numStores; x++) {
        int ID = nextInt() - 1;
        int cost = nextInt();
        pencilCosts[ID] = cost;
      }
      int destination = nextInt() - 1;
      // if (isGood[destination]){

      // }
      int minCost = 100000000;

      Queue<Node> Q = new LinkedList<Node>();
      // PriorityQueue<Node> Q = new PriorityQueue<Node>();
      minDistance[destination] = 0;
      // cities[destination].distance = 0;
      Q.offer(cities[destination]);
      while (!Q.isEmpty()) {
        Node temp = Q.poll();
        for (int x = 0; x < numCities; x++) {
          if (adjacencyMatrix[temp.ID][x] != 0
              && (minDistance[x] == 100000000
                  || minDistance[x] > minDistance[temp.ID] + adjacencyMatrix[temp.ID][x])) {
            minDistance[x] = minDistance[temp.ID] + adjacencyMatrix[temp.ID][x];
            if (pencilCosts[x] != -1 && minDistance[x] < minCost) {
              // System.out.println(minCost);
              minCost = Math.min(minDistance[x] + pencilCosts[x], minCost);
              Q.offer(cities[x]);
            } else {
              if (pencilCosts[x] == -1) { // why>
                Q.offer(cities[x]);
              }
            }
            // Q.offer(temp.routes.get(x).destination);
          }
        }
      }

      for (int x = 0; x < numCities; x++) {
        if (pencilCosts[x] != -1
            && pencilCosts[x] + minDistance[x] < minCost
            && minDistance[x] != 100000000) {
          minCost = minDistance[x] + pencilCosts[x];
        }
      }
      System.out.println(minCost);

    } catch (IOException e) {
      System.out.println("IO: General");
    }
  }
Example #15
0
 public String winner(int[] x, int[] y) {
   int n = x.length;
   for (int i = 0; i < n; ++i) {
     x[i] += MAX_ABS;
     y[i] += MAX_ABS;
   }
   ArrayList<Point> points = new ArrayList<Point>();
   int oneCount = 0;
   int twoCount = 0;
   for (int x0 = 0; x0 <= MAX_ABS * 2; ++x0) {
     boolean found = false;
     double yMin = Double.MAX_VALUE;
     double yMax = Double.MIN_VALUE;
     for (int i = 0; i < n; ++i) {
       int x1 = x[i];
       int y1 = y[i];
       int x2 = x[(i + 1) % n];
       int y2 = y[(i + 1) % n];
       if ((long) (x1 - x0) * (x2 - x0) <= 0) {
         found = true;
         if (x1 == x2) {
           yMin = Math.min(yMin, Math.min(y1, y2));
           yMax = Math.max(yMax, Math.max(y1, y2));
         } else {
           double y0 = y1 + (double) (y2 - y1) / (x2 - x1) * (x0 - x1);
           yMin = Math.min(yMin, y0);
           yMax = Math.max(yMax, y0);
         }
       }
     }
     if (found) {
       int count = 0;
       int y0 = (int) Math.ceil(yMin);
       while (count < 2 && y0 <= yMax) {
         boolean valid = true;
         for (int i = 0; i < n; ++i) {
           valid &= x[i] != x0 || y[i] != y0;
         }
         if (valid) {
           count++;
           points.add(new Point(x0, y0));
         }
         y0++;
       }
       if (count >= 1) {
         oneCount++;
       }
       if (count >= 2) {
         twoCount++;
       }
     }
   }
   if (twoCount > 0) {
     return oneCount > 1 ? YES : NO;
   }
   if (points.size() <= 2) {
     return NO;
   }
   Point o = points.get(0);
   for (int i = 2; i < points.size(); ++i) {
     if (points.get(1).subtract(o).det(points.get(i).subtract(o)) != 0) {
       return YES;
     }
   }
   return NO;
 }
Example #16
0
  /**
   * In Oak it is possible to embed a tab character as an ASCII 8 or as a \t but internally we store
   * this in ASCII. The same goes for newlines and other tables (see the Oak reference). Here we
   * convert external format to internal format.
   *
   * @param source The text to process, may be null. E.g. "Hello\tworld" (with the quotes)
   * @return Return an internal format string.
   */
  @Nullable
  public static String textToInternalFormat(@Nullable String source) throws ParsingException {
    final String result;

    assert source == null
        || source.length() >= 2
            && source.charAt(0) == '"'
            && source.charAt(source.length() - 1) == '"';

    if (source == null) {
      result = null;
    } else {
      final Text t = new Text();
      final char[] ca = source.toCharArray();
      final int length = ca.length - 1; // 1 because remove quotes

      for (int i = 1; i < length; i++) { // 1 because remove quotes
        final char c = ca[i];
        if (c == '\\') {
          if (i == length - 1) {
            error("Invalid text " + source + ", escape at end of line");
          } else {
            i++;
            final char next = ca[i];
            if (next == 't') {
              t.append('\t');
            } else if (next == 'n') {
              t.append('\n');
            } else if (next == '"') {
              t.append('"');
            } else if (next == '\\') {
              t.append('\\');
            } else if (next == 'u') {
              // Unicode, 1-4 hex characters...
              i++;

              final Text hex = new Text();
              hex.append(source);
              hex.setCursor(i);
              final int start = i;
              if (hex.consumeAscii(Text.ASCII_0_F)) {
                final int end = start + Math.min(4, hex.cursor() - start);
                final String string = hex.getString(start, end);
                final char u = (char) Integer.parseInt(string, 16);
                t.append(u);
                i = end - 1;
              } else {
                error("Invalid text " + source + ", incorrect unicode");
              }
            } else {
              error("Invalid text: \\" + next);
            }
          }
        } else {
          t.append(c);
        }
      }
      result = t.toString();
    }

    return result;
  }
 // To add/remove functions change evaluateOperator() and registration
 public double evaluateFunction(String fncnam, ArgParser fncargs) throws ArithmeticException {
   switch (Character.toLowerCase(fncnam.charAt(0))) {
     case 'a':
       {
         if (fncnam.equalsIgnoreCase("abs")) {
           return Math.abs(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("acos")) {
           return Math.acos(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("asin")) {
           return Math.asin(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("atan")) {
           return Math.atan(fncargs.next());
         }
       }
       break;
     case 'c':
       {
         if (fncnam.equalsIgnoreCase("cbrt")) {
           return Math.cbrt(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("ceil")) {
           return Math.ceil(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("cos")) {
           return Math.cos(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("cosh")) {
           return Math.cosh(fncargs.next());
         }
       }
       break;
     case 'e':
       {
         if (fncnam.equalsIgnoreCase("exp")) {
           return Math.exp(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("expm1")) {
           return Math.expm1(fncargs.next());
         }
       }
       break;
     case 'f':
       {
         if (fncnam.equalsIgnoreCase("floor")) {
           return Math.floor(fncargs.next());
         }
       }
       break;
     case 'g':
       {
         //              if(fncnam.equalsIgnoreCase("getExponent"   )) { return
         // Math.getExponent(fncargs.next());                } needs Java 6
       }
       break;
     case 'l':
       {
         if (fncnam.equalsIgnoreCase("log")) {
           return Math.log(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("log10")) {
           return Math.log10(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("log1p")) {
           return Math.log1p(fncargs.next());
         }
       }
       break;
     case 'm':
       {
         if (fncnam.equalsIgnoreCase("max")) {
           return Math.max(fncargs.next(), fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("min")) {
           return Math.min(fncargs.next(), fncargs.next());
         }
       }
       break;
     case 'n':
       {
         //              if(fncnam.equalsIgnoreCase("nextUp"        )) { return Math.nextUp
         // (fncargs.next());                } needs Java 6
       }
       break;
     case 'r':
       {
         if (fncnam.equalsIgnoreCase("random")) {
           return Math.random();
         } // impure
         if (fncnam.equalsIgnoreCase("round")) {
           return Math.round(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("roundHE")) {
           return Math.rint(fncargs.next());
         } // round half-even
       }
       break;
     case 's':
       {
         if (fncnam.equalsIgnoreCase("signum")) {
           return Math.signum(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("sin")) {
           return Math.sin(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("sinh")) {
           return Math.sinh(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("sqrt")) {
           return Math.sqrt(fncargs.next());
         }
       }
       break;
     case 't':
       {
         if (fncnam.equalsIgnoreCase("tan")) {
           return Math.tan(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("tanh")) {
           return Math.tanh(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("toDegrees")) {
           return Math.toDegrees(fncargs.next());
         }
         if (fncnam.equalsIgnoreCase("toRadians")) {
           return Math.toRadians(fncargs.next());
         }
       }
       break;
     case 'u':
       {
         if (fncnam.equalsIgnoreCase("ulp")) {
           return Math.ulp(fncargs.next());
         }
       }
       break;
       // no default
   }
   throw new UnsupportedOperationException(
       "MathEval internal function setup is incorrect - internal function \""
           + fncnam
           + "\" not handled");
 }