Problem solution · Python

ABC324 E — Joint Two Strings

ABC324 E — Joint Two Strings: a Python solution using sliding window or two pointers. Learn the idea, check the complexity, and read the full code, with credit to KATO-Hiro AtCoder Solutions.

Technique
Sliding window or two pointers
Source
KATO-Hiro AtCoder Solutions
Length
73 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Sliding window or two pointers

For ABC324 E — Joint Two Strings, the implementation maintains a moving interval and updates only the information that enters or leaves the window.

  1. Choose the invariant that makes a window valid or useful.
  2. Advance the right boundary and add the new element.
  3. Move the left boundary only as needed while maintaining the invariant and updating the answer.

Code notes

  • 73 lines of Python from the credited upstream file abc324_e.py.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from KATO-Hiro AtCoder Solutions by KATO-Hiro and is used under the CC0-1.0 licence.

Full codeABC324 E — Joint Two Strings · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*- from bisect import bisect_left, bisect_rightfrom typing import List  def bisect_ge(sorted_array: List[int], value: int):    """Find the smallest element >= x and its index, or None if it doesn't exist."""     if sorted_array[-1] >= value:        index: int = bisect_left(sorted_array, value)         return index, sorted_array[index]     return None, None  def main():    import sys     input = sys.stdin.readline     n, t = input().rstrip().split()    n = int(n)    m = len(t)    t_rev = t[::-1]    left, right = list(), list()     # Siの前と後ろから、tとどこまで一致するかGreedyに求める    for _ in range(n):        si = input().rstrip()         i = 0         for sij in si:            while i < m and sij == t[i]:                i += 1                break         left.append(i)         j = 0         for sij in si[::-1]:            while j < m and sij == t_rev[j]:                j += 1                break         right.append(j)     # print(left)    # print(right)     # 左側の一致する個数Li + 右側の一致する個数Rjが|T|以上なら条件を満たす    # LとRを独立に考えて、上記の条件を満たす場合を数える    # Lを全探索、Rを二分探索    right.sort()    ans = 0     for li in left:        j, value = bisect_ge(right, m - li)  # the smallest element >= x         if j is not None:            count = n - j            ans += count            # print(j, count)     print(ans)  if __name__ == "__main__":    main() 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗