// 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 #2
0
 boolean solve2() {
   int t = nextInt();
   if (t == 0) return false;
   int n = nextInt();
   Tower[] towers = new Tower[t];
   for (int i = 0; i < t; i++) {
     towers[i] = new Tower(new Point(nextInt(), nextInt()), nextInt());
   }
   n++;
   Point[] p = new Point[n];
   for (int i = 0; i < n; i++) {
     p[i] = new Point(nextInt(), nextInt());
   }
   // System.err.println(Arrays.toString(p));
   ArrayList<Point> points = new ArrayList<Point>();
   double r = 1;
   for (int i = 0; i < n - 1; i++) {
     double dist = p[i].dist(p[i + 1]) - (1 - r);
     int e = (int) Math.floor(dist + 1 - EPS);
     Point v = p[i + 1].subtract(p[i]).norm();
     for (int j = 0; j < e; j++) {
       points.add(p[i].add(v.multiply(j).add(v.multiply(1 - r))));
     }
     r = p[i + 1].dist(points.get(points.size() - 1));
   }
   if (p[n - 1].dist(points.get(points.size() - 1)) > 0.5 - EPS) {
     points.add(p[n - 1]);
   }
   // System.err.println(points);
   char last = 0;
   ArrayList<String> ans = new ArrayList<String>();
   for (int i = 0; i < points.size(); i++) {
     double maxP = Integer.MIN_VALUE;
     char here = 0;
     for (int j = 0; j < t; j++) {
       double w = towers[j].get(points.get(i));
       if (maxP < w - EPS) {
         maxP = w;
         here = (char) (j + 'A');
       }
     }
     if (here != last) {
       ans.add("(" + i + "," + here + ")");
     }
     last = here;
   }
   for (int i = 0; i < ans.size(); i++) {
     if (i != 0) out.print(" ");
     out.print(ans.get(i));
   }
   out.println();
   return true;
 }
Example #3
0
  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());
  }
Example #4
0
 double solve(Point[] points) {
   int n = points.length;
   Arrays.sort(points);
   Point[] down = getConvex(points);
   Point[] up = reverse(getConvex(reverse(points)));
   int xMin = down[0].x;
   int xMax = down[down.length - 1].x;
   long total = 0;
   double sum = 0.0;
   double squareSum = 0.0;
   for (int x = xMin, i = 0, j = 0; x <= xMax; ++x) {
     while (i + 1 < down.length && down[i + 1].x < x) {
       i++;
     }
     while (j + 1 < up.length && up[j + 1].x < x) {
       j++;
     }
     int yMin =
         (int)
                 Math.ceil(
                     (double) (x - down[i].x)
                         / (down[i + 1].x - down[i].x)
                         * (down[i + 1].y - down[i].y))
             + down[i].y;
     int yMax =
         (int)
                 Math.floor(
                     (double) (x - up[j].x) / (up[j + 1].x - up[j].x) * (up[j + 1].y - up[j].y))
             + up[j].y;
     int count = yMax - yMin + 1;
     total += count;
     sum += (double) count * x;
     squareSum += (double) count * x * x;
   }
   return (total * squareSum - sum * sum) / ((double) total * (total - 1));
 }
Example #5
0
 /** Rounds the specified number. */
 public static double round(double num, int d) {
   // if decimal is 0.5, rounds up if d positive; down if d is negative
   return (d >= 0) ? Math.floor(num + 0.5) : Math.ceil(num - 0.5);
 }
 // 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");
 }