Official

A - 数直線上の出会い / Meeting on a Number Line Editorial by admin

gpt-5.3-codex

Overview

This is a problem where you simulate the current positions of two people minute by minute at each time step, and count the number of times their coordinates coincide.
It can be solved by converting the characters L/R/S into movement amounts of \(-1/+1/0\) respectively, and updating positions sequentially.

Analysis

The key point is that “meetings” are counted only at integer time instants \(i\).
In other words, you check the coordinates after each minute’s movement, and count it once if they are equal. Even if they pass each other in between, it is ignored.

For example, if during a certain minute: - Takahashi: 0 → 1 - Aoki: 1 → 0
they cross paths, but since their positions do not match at the end of that time step, it does not count as a meeting.


A naive approach of “storing all positions at each time step in separate arrays and then comparing” would work, but storing them is unnecessary.
All you need is “Takahashi’s current coordinate,” “Aoki’s current coordinate,” and “the meeting count,” so a single pass is sufficient.

The constraint is \(T \le 10^6\), so: - Processing each time step once in \(O(T)\) is fast enough - A double loop of \(O(T^2)\) is obviously not feasible

Algorithm

  1. Prepare Takahashi’s current position tx = X, Aoki’s current position ay = Y, and meeting count meet = 0.
  2. Repeat for \(i=0\) to \(T-1\):
    • Update tx according to A[i] (L:-1, R:+1, S:0)
    • Update ay according to B[i]
    • After updating, if tx == ay, then meet += 1
  3. Output meet at the end.

Defining a delta() function for the movement conversion makes the implementation cleaner.

Complexity

  • Time complexity: \(O(T)\)
  • Space complexity: \(O(1)\) (excluding the input strings)

Implementation Notes

  • According to the problem statement, time 0 is not counted, so we do not compare at the initial state. The check is performed after movement inside the loop.

  • S has a movement amount of 0. An implementation where the delta function treats anything other than L/R as 0 is concise.

  • Since the input size can be large, using sys.stdin.readline ensures stable and fast performance.

    Source Code

import sys

def delta(c: str) -> int:
    if c == 'L':
        return -1
    if c == 'R':
        return 1
    return 0  # 'S'

def main():
    input = sys.stdin.readline
    T, X, Y = map(int, input().split())
    A = input().strip()
    B = input().strip()

    tx = X
    ay = Y
    meet = 0

    for i in range(T):
        tx += delta(A[i])
        ay += delta(B[i])
        if tx == ay:
            meet += 1

    print(meet)

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.3-codex.

posted:
last update: