#C5464. Find the Sublist Index

    ID: 49116 Type: Default 1000ms 256MiB

Find the Sublist Index

Find the Sublist Index

You are given two lists of integers: a main list and a sublist. Your task is to find the starting index of the first occurrence of the sublist within the main list.

If the sublist is not found, output -1. Note that an empty sublist is always considered to be found at index 0. The problem can be mathematically formalized as follows:

Given two sequences \(A = [a_0,a_1,\dots,a_{n-1}]\) and \(B = [b_0,b_1,\dots,b_{m-1}]\), find the smallest index \(i\) such that \[ (a_i, a_{i+1}, \dots, a_{i+m-1}) = (b_0,b_1,\dots,b_{m-1}) \] If no such index exists, print -1. If \(B\) is empty, print 0.

The input is taken from standard input and the result should be printed to standard output.

inputFormat

The input consists of two lines:

  • The first line contains the elements of the main list separated by spaces. If the line is empty, the main list is empty.
  • The second line contains the elements of the sublist separated by spaces. If the line is empty, the sublist is empty.

You may assume that all numbers are integers.

outputFormat

Print a single integer: the starting index of the first occurrence of the sublist within the main list. If the sublist is not found that way, print -1.

## sample
1 2 3 4 5
3 4
2