#P8085. Earliest Match of Ciphertext in Plaintext
Earliest Match of Ciphertext in Plaintext
Earliest Match of Ciphertext in Plaintext
Given a plaintext and a ciphertext, both consisting of English words, find the earliest possible position in the plaintext at which the given ciphertext can be mapped by a substitution cipher. In this cipher, each word in the ciphertext corresponds to a unique word in the plaintext. Formally, let \( n \) be the number of words in the plaintext and \( m \) be the number of words in the ciphertext. For a candidate segment of \( m \) contiguous words in the plaintext starting at position \( i \) (1-indexed), there exists a mapping \( f \) from ciphertext words to plaintext words such that for every index \( j \) (\( 1 \leq j \leq m \)), if a ciphertext word appears multiple times then the corresponding plaintext words must be identical, and distinct ciphertext words cannot map to the same plaintext word. Output the smallest \( i \) that satisfies these conditions, or \(-1\) if no valid segment exists.
inputFormat
The input consists of two lines:
- The first line contains the plaintext, a sequence of English words separated by spaces.
- The second line contains the ciphertext, a sequence of English words separated by spaces.
Both lines have at least one word.
outputFormat
Output a single integer: the starting position (1-indexed) in the plaintext where the ciphertext can be mapped under a consistent substitution. If no such segment exists, output -1
.
sample
this is a test
a test
3