Official

A - 注文の確認 / Order Confirmation Editorial by admin

GPT 5.4 High

Overview

For each customer, compare the actual order \(T_i\) with the order conveyed to the kitchen \(S_i\) one pair at a time, and count the number of times they differ.

Analysis

The key point of this problem is to determine correctness independently for each customer.

For example:

  • Customer 1: actual order is ramen, conveyed order is also ramen
  • Customer 2: actual order is sushi, conveyed order is pasta

In this case, only customer 2 has an error, so the answer is \(1\).

Key Observation

For the \(i\)-th customer, all we need is:

  • If \(T_i = S_i\), the order was conveyed correctly
  • If \(T_i \neq S_i\), the order was conveyed incorrectly

This is a simple check.

In other words, overall we just need to: Count the number of pairs where \(T_i\) and \(S_i\) differ.

A Common Mistake

Looking only at the frequency of dish names leads to an incorrect solution.

For example:

  • Actual orders: a, b
  • Conveyed orders: b, a

In this case, the types and counts of dish names are the same when viewed as a whole, but both customer 1 and customer 2 have mismatched orders, so the answer is \(2\).

Therefore, you need to compare line by line, matching them in order.

How to Solve

Each time you read a line, compare the strings \(T_i\) and \(S_i\). If they differ, increment the answer by \(1\). Repeat this for all \(N\) customers to get the result.

Algorithm

  1. Read \(N\)
  2. Initialize a variable ans to \(0\)
  3. Repeat \(N\) times:
    • Read strings t and s
    • If t != s, then ans += 1
  4. Output ans

Complexity

  • Time complexity: \(O(N)\)
  • Space complexity: \(O(1)\)

※ Since the maximum string length is \(20\) characters, the cost of string comparison can be considered constant.

Implementation Notes

  • There is no need to store all orders in an array. It is sufficient to compare and count as you read each line.

  • In Python, you can directly compare strings using !=.

  • Since the input can be large, it is safer to use fast input by setting input = sys.stdin.readline.

    Source Code

import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    ans = 0
    for _ in range(N):
        t, s = input().split()
        if t != s:
            ans += 1
    print(ans)

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.4-high.

posted:
last update: