Блог пользователя Bliss8

Автор Bliss8, 6 лет назад, По-английски

Recently there was a problem DIV2 A to count subsequences 'QAQ' in given string

There is O(N) solution for that problem: count how many 'Q' we have on a prefix[i], and then for each 'A' res+=prefix[i]*(qTotal - prefix[i]). (multiply number of 'Q's to the left and right from 'A')

But what if we had to count 'QAQAQ' or any other sequence? I would like to get some insights on general solution of the problem.

Полный текст и комментарии »

  • Проголосовать: нравится
  • +7
  • Проголосовать: не нравится

Автор Bliss8, 7 лет назад, По-английски

Given small string s and bigger string b
Need to find all permutations of string s in b. How to do it in O(|b|)?

example:
s = "ab" b = "babba"

output: "ba", "ab", "ba"

Полный текст и комментарии »

  • Проголосовать: нравится
  • +5
  • Проголосовать: не нравится

Автор Bliss8, 7 лет назад, По-английски

Given a problem:
find diameter of binary tree (diameter is longest possible path between any two nodes in a tree)
Node format:

public class TreeNode {
    TreeNode left;
    TreeNode right;
}

External variable approach:

int maxDiameter = 0; //we will update this external variable from recursion calls
public int diameterOfBinaryTree(TreeNode root) {
    explore(root);
    return maxDiameter;
}

public int explore(TreeNode node) {
    if (node == null) {
        return 0;
    }
    int left = explore(node.left);
    int right = explore(node.right);
    maxDiameter = Math.max(maxDiameter, left + right);
    return Math.max(left, right) +  1;
}

Composite return approach:
To solve this task in clear functional way, without external variables, need to return information about max diameter found instead of updating a variable. As now it's needed to return 2 values (max diameter found and depth), will simply return int[].

public int diameterOfBinaryTree(TreeNode root) {
    return explore(root)[1];
}

public int[] explore(TreeNode node) {
    if (node == null) {
        return new int[]{0, 0};
    }
    int[] left = explore(node.left);
    int[] right = explore(node.right);
    int maxDepth = Math.max(left[0], right[0]);
    int maxDiameter = Math.max(Math.max(left[1], right[1]), left[0] + right[0]);
    return new int[]{maxDepth + 1, maxDiameter};
}

Which approach do you prefer?
From my point of view, if mutability is acceptable, external variable is the way to go.

Полный текст и комментарии »

  • Проголосовать: нравится
  • +2
  • Проголосовать: не нравится

Автор Bliss8, история, 7 лет назад, По-английски

question example:
given a string "aaaabbbasd" and dictionary, for example {a, aa, aaa, b, bb, s, d}
1) find one possible way to split the string into words from dictionary
2) find all possible ways to split the string into words from dictionary

Searching for the best approach on web I found different ones: dp, backtracking + memorization, dfs + memorization.
I lost in all of this huge variety.
Does somebody know what approach is the best here?

Полный текст и комментарии »

  • Проголосовать: нравится
  • -5
  • Проголосовать: не нравится

Автор Bliss8, история, 8 лет назад, По-английски

Is there a way to find some post if the only thing i remember is approximate date when it was visible in "Recent actions"? Seems like CF is lacking of such functionality.

Полный текст и комментарии »

  • Проголосовать: нравится
  • +12
  • Проголосовать: не нравится

Автор Bliss8, история, 8 лет назад, По-русски

Привет. Хочу научиться решать задачи на отрезках. Помогите найти обучающие материалы.

Нашел в тренировках условия к задачам, но нет эдиториалов http://codeforces.com/gym/100093.

Посоветуйте пожалуйста материалы для обучения.

Полный текст и комментарии »

  • Проголосовать: нравится
  • +6
  • Проголосовать: не нравится