- Choose the invariant that makes a window valid or useful.
- Advance the right boundary and add the new element.
- 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.
Use this to learn the idea, then write your own version.
12 3from bisect import bisect_left, bisect_right4from typing import List5 6 7def bisect_ge(sorted_array: List[int], value: int):8 """Find the smallest element >= x and its index, or None if it doesn't exist."""9 10 if sorted_array[-1] >= value:11 index: int = bisect_left(sorted_array, value)12 13 return index, sorted_array[index]14 15 return None, None16 17 18def main():19 import sys20 21 input = sys.stdin.readline22 23 n, t = input().rstrip().split()24 n = int(n)25 m = len(t)26 t_rev = t[::-1]27 left, right = list(), list()28 29 30 for _ in range(n):31 si = input().rstrip()32 33 i = 034 35 for sij in si:36 while i < m and sij == t[i]:37 i += 138 break39 40 left.append(i)41 42 j = 043 44 for sij in si[::-1]:45 while j < m and sij == t_rev[j]:46 j += 147 break48 49 right.append(j)50 51 52 53 54 55 56 57 right.sort()58 ans = 059 60 for li in left:61 j, value = bisect_ge(right, m - li) 62 63 if j is not None:64 count = n - j65 ans += count66 67 68 print(ans)69 70 71if __name__ == "__main__":72 main()73