Exemplo n.º 1
0
  public static void main(String[] args) {
    InputReader in = new InputReader(System.in);
    OutputWriter out = new OutputWriter(System.out);
    int tc = in.nextInt();
    while (tc-- > 0) {
      String a = in.nextString();
      String b = in.nextString();
      int[][] dp = new int[2][b.length() + 1];
      dp[1][0] = 1;
      for (int i = 0; i <= b.length(); i++) {
        dp[0][i] = i;
      }

      for (int i = 1, r = a.length(); i <= r; i++) {
        dp[i % 2][0] = i;
        for (int j = 1, c = b.length(); j <= c; j++) {
          if (a.charAt(i - 1) == b.charAt(j - 1)) {
            dp[i % 2][j] = dp[(i - 1) % 2][j - 1];
          } else {
            dp[i % 2][j] = 1 + min(dp[(i - 1) % 2][j], dp[(i - 1) % 2][j - 1], dp[i % 2][j - 1]);
          }
        }
      }

      out.println(dp[a.length() % 2][b.length()]);
    }

    out.flush();
    out.close();
  }
Exemplo n.º 2
0
 public void solve(int testNumber, InputReader in, OutputWriter out) {
   int n = in.nextInt(), m = in.nextInt();
   boolean[] tab = new boolean[255 * 255 * 2];
   for (int p = 0; p <= m; ++p) {
     for (int q = 0; q <= p; ++q) {
       tab[p * p + q * q] = true;
     }
   }
   List<Integer> lst = new ArrayList<Integer>();
   for (int i = 0; i < tab.length; ++i) {
     if (tab[i]) {
       lst.add(i);
     }
   }
   boolean found = false;
   int b = 1;
   while (true) {
     if ((n - 1) * b > m * m * 2) {
       break;
     }
     for (int idx = 0; idx + n <= lst.size(); ++idx) {
       int a = lst.get(idx);
       if (a + (n - 1) * b > m * m * 2) {
         break;
       }
       // out.println(a, b);
       boolean flag = true;
       for (int i = n - 1; i >= 0; --i) {
         int x = a + i * b;
         if (!tab[x]) {
           flag = false;
           break;
         }
       }
       if (flag) {
         out.println(a, b);
         found = true;
       }
     }
     b++;
   }
   if (!found) {
     out.println("NONE");
   }
 }
Exemplo n.º 3
0
  public static void main(String[] args) {
    InputReader in = new InputReader(System.in);
    OutputWriter out = new OutputWriter(System.out);
    int tc = in.nextInt();
    while (tc-- > 0) {
      int n = in.nextInt();
      out.println(countTri(n));
    }

    out.flush();
    out.close();
  }
    void solve() {
      n = in.nextInt();
      m = in.nextInt();
      days = new int[n + 1];
      req = new int[m + 1];

      for (int i = 1; i <= n; i++) days[i] = in.nextInt();

      for (int i = 1; i <= m; i++) req[i] = in.nextInt();

      if (!isPossible(n)) {
        out.println(-1);

        return;
      }

      int low, high, mid;

      low = 1;
      high = n;
      mid = 1;

      while (low <= high) {
        mid = low + high >> 1;

        boolean poss = isPossible(mid);

        if (poss) {
          boolean prev = isPossible(mid - 1);

          if (prev) high = mid - 1;
          else break;
        } else low = mid + 1;
      }

      out.println(mid);
    }
Exemplo n.º 5
0
  public static void main(String[] args) {
    InputReader in = new InputReader(System.in);
    OutputWriter out = new OutputWriter(System.out);
    while (true) {
      int n = in.nextInt();
      if (n == 0) break;
      int c = 0;
      while (n > 1) {
        n /= 2;
        c++;
      }

      out.println(c);
    }
    out.flush();
    out.close();
  }
    void solve() {
      n = in.nextInt();
      k = in.nextInt();
      bit = new int[n + 1];

      for (int i = 0; i < k; i++) {
        int a, b;

        a = in.nextInt();
        b = in.nextInt();
        add(a, 1);
        add(b + 1, -1);
      }

      int[] ans = new int[n];

      for (int i = 1; i <= n; i++) ans[i - 1] = query(i);

      Arrays.sort(ans);
      out.println(ans[n / 2]);
    }
    void solve(int testNumber) {
      n = in.nextInt();
      gain = new int[n];
      lose = new int[n];

      for (int i = 0; i < n; i++) gain[i] = in.nextInt();

      for (int i = 0; i < n; i++) lose[i] = in.nextInt();

      int max = 0;
      int maxStartingPos = -1;
      boolean isInfinity = false;
      int infinityFrom = -1;
      int crossedN = 0;

      outer:
      for (int start = 0; start < n; ) {
        int curr = gain[start];
        int startingPoint = start;
        int currLen = 1;
        System.out.println(
            "start : "
                + start
                + ", currLen : "
                + currLen
                + ", currMaxLen : "
                + max
                + ", sP : "
                + startingPoint);

        while (start < n && curr >= lose[start]) {
          curr -= lose[start];
          start++;
          currLen++;

          if (start == startingPoint) {
            isInfinity = true;
            infinityFrom = start;

            break outer;
          }

          if (start == n) {
            start = 0;
            crossedN++;
          }

          curr += gain[start];
        }

        if (currLen > max) {
          max = currLen;
          maxStartingPos = startingPoint;
        }

        start++;

        if (start >= n) {
          crossedN++;

          if (crossedN >= 2) break;
        }
      }

      if (isInfinity) out.println("Infinity, from position " + infinityFrom);
      else out.println("Max dist from position " + maxStartingPos);
    }
  static void solve() {
    n = in.nextInt();

    arr = new int[1001][1001];

    for (int i = 0; i < n; i++) {
      int x, y;

      x = in.nextInt() - 1;
      y = in.nextInt() - 1;

      arr[x][y]++;
    }

    int i, j;
    int start = 0;
    long left, right, answer;

    left = right = answer = 0;

    while (start < 1000) {
      i = 0;
      j = start;
      left = 0;

      while (i < 1000 && j < 1000) {
        if (arr[i][j] > 0) left++;

        i++;
        j++;
      }

      answer += (left * (left - 1) / 2);
      start++;
    }

    start = 1;

    while (start < 1000) {
      i = start;
      j = 0;
      left = 0;

      while (i < 1000 && j < 1000) {
        if (arr[i][j] > 0) left++;

        i++;
        j++;
      }

      answer += (left * (left - 1) / 2);
      start++;
    }

    start = 999;

    while (start >= 0) {
      i = start;
      j = 0;
      right = 0;

      while (i >= 0 && j < 1000) {
        if (arr[i][j] > 0) right++;

        i--;
        j++;
      }

      answer += (right * (right - 1) / 2);
      start--;
    }

    start = 1;

    while (start < 1000) {
      i = 999;
      j = start;
      right = 0;

      while (i >= 0 && j < 1000) {
        if (arr[i][j] > 0) right++;

        i--;
        j++;
      }

      answer += (right * (right - 1) / 2);
      start++;
    }

    out.println(answer);
  }