Implementing a Prefix Tree Data Structure
Understanding Prefix Trees
A prefix tree, commonly known as a Trie, is a specialized tree structure designed for efficient string storage and retrieval. Each node in a Trie represents a character, and the path from the root to a node represents a prefix. This structure excels in applications like autocomplete systems and spell checkers.
Initia ...
Posted on Thu, 29 Oct 2026 16:01:32 +0000 by jonniejoejonson
Winter Algorithm Bootcamp 2025 – Session 3 Solutions
Chino’s Take-Away Game
The game ends when only one stone remains. If the initial pile contains an odd count, the first player can always remove exactly one stone, leaving an even number. Any subsequent move by the second player must remove an odd amount (co-prime with the even remainder), again leaving an odd count. By induction the first playe ...
Posted on Mon, 05 Oct 2026 16:32:04 +0000 by Batosi
Replacing Words with Their Shortest Root Prefix
Problem Description
In English, there exists a concept called root which can be combined with other words to form longer words called successors. To example, the root "an" combined with "other" forms the word "another".
Given a dictionary of roots and a sentence, replace all successor words in the sentence with the ...
Posted on Thu, 01 Oct 2026 16:10:25 +0000 by sgalatas
Manacher's Algorithm and AC Automaton: Linear-Time Palindromes and Multi-Pattern Matching
Manacher's Algorithm
Purpose
Manacher's algorithm computes the longest palindromic substring centered at each position (including positions between characters for even-length palindromes) in O(n) time complexity.
Naive Approach
The naive method examines each center position and attempts to expand outward character by character until the charact ...
Posted on Sat, 19 Sep 2026 16:16:48 +0000 by Mathy
Resolving ValueError: prefix_allowed_tokens_fn Returned Empty List in HuggingFace Transformers
When using the transformers library for text generation with the prefix_allowed_tokens_fn parameter to constrain model outputs, a specific error may appear intermittently:
ValueError: prefix_allowed_tokens_fn returned an empty list for batch ID 0. This means that the constraint is unsatisfiable. Please check your implementation of prefix_allow ...
Posted on Sat, 12 Sep 2026 16:42:48 +0000 by ace21
National Day Simulation Contest Solutions
T1
This is a straightforward problem. Key reminder: read the problem carefully! Simpler problems are prone to errors.
T2
This is a straightforward problem. Greedy algorithms or dynamic programming can be used.
T3
Tip: When dealing with averages, subtract the average from all numbers and find subarrays with sum zero. Since the value range is sma ...
Posted on Sat, 12 Sep 2026 16:27:13 +0000 by hossein2kk
Efficient Calculation of String Sorting Distance Using Tries and 2D Range Queries
Problem Definition
Given $n$ strings $T_1, T_2, \dots, T_n$, each of length $len$. Define $f(a, b)$ as the minimum number of sorting operations required on substrings of $a$ to make it identical to $b$. If it is impossible to transform $a$ into $b$ via substring sorting, $f(a, b) = 1337$. The objective is to compute:
Analysis of Function Value ...
Posted on Mon, 07 Sep 2026 16:43:49 +0000 by ninedoors
Optimizing String and Array Problems with Greedy Algorithms and Data Structures
Problem 1: Lexicographical String Matching
Solution
Since the problem involves lexicographical order, a trie data structure is suitable.
To find the solution, use a greedy approach. Determine if the string ending at the current node is the answer. If not, continue to traverse to one of the child nodes. The process is illustrated in the followi ...
Posted on Wed, 02 Sep 2026 16:37:10 +0000 by corbin
Efficient Algorithms for Range Queries, Matrix Exponentiation, Trie DFS, and Subset Sum with Modular Arithmetic
Problem T1: Maximum Cross-Shaped Area in a Grid
Given a binary grid where '.' represents a valid cell and other characters are blocked, compute the largest cross-shaped region centered at any valid cell. A cross is defined by a vertical segment of height h and a horizontal segment of width w, both centered at the same point, with the total peri ...
Posted on Wed, 26 Aug 2026 16:40:14 +0000 by ricerocket
Educational Codeforces Round 157 Div. 2: Virtual Contest Analysis
Problem A: Treasure Chest
We need to calculate the minimum time to reach the chest and return to the start, given the ability to pull the chest towards the key for a maximum distance of k. There are two scenarios based on the relative positions of the chest pos_chest and the key pos_key:
If pos_key <= pos_chest: We pickup the key on the way ...
Posted on Mon, 17 Aug 2026 16:45:03 +0000 by vicodin