Official

A - パスワード照合 / Password Verification Editorial by admin

DeepSeek V3

Overview

This problem asks you to determine whether candidate strings satisfy the conditions for a password where some characters are already known.

Analysis

The core of the problem is to check whether characters match only at the recovered positions (\(P_i\)), while allowing any character at all other positions. A straightforward approach would check all recovered positions for each candidate string. Since \(M\) can be up to \(10^5\) and \(Q\) can be up to \(1000\), the worst case requires \(10^5 \times 10^3 = 10^8\) comparisons. In Python, due to large constant factors, this could potentially result in TLE (Time Limit Exceeded) in the worst case. However, in practice, cases where \(M\) is \(0\) or where mismatches are found early are common, so in reality it runs sufficiently fast.

Algorithm

  1. Read \(N\), \(M\), \(Q\) from input
  2. Store the recovered data in a dictionary (hash map). The key is position \(P_i\), and the value is character \(C_i\)
  3. For each candidate string \(T_j\):
    • For all positions registered in the dictionary, check whether the corresponding character in the candidate string matches \(C_i\)
    • If even one mismatch is found, output No; if all match, output Yes

Complexity

  • Time complexity: \(O(M \cdot Q)\) (worst case)
  • Space complexity: \(O(M + N + Q)\)

Implementation Notes

  • By managing recovered data with a dictionary, fast access by position is achieved

  • Early break terminates the check as soon as a mismatch is found, reducing unnecessary comparisons

  • Index adjustment (\(pos-1\)) converts 1-indexed position information to 0-indexed array indices

    Source Code

import sys

def main():
    data = sys.stdin.read().splitlines()
    first_line = data[0].split()
    N = int(first_line[0])
    M = int(first_line[1])
    Q = int(first_line[2])
    
    known = {}
    for i in range(1, 1 + M):
        parts = data[i].split()
        pos = int(parts[0])
        char = parts[1]
        known[pos] = char
        
    output_lines = []
    for i in range(1 + M, 1 + M + Q):
        candidate = data[i].strip()
        valid = True
        for pos, char in known.items():
            idx = pos - 1
            if candidate[idx] != char:
                valid = False
                break
                
        output_lines.append("Yes" if valid else "No")
    
    print("\n".join(output_lines))

if __name__ == "__main__":
    main()

This editorial was generated by deepseekv3.

posted:
last update: