IP Matching

면접 대비

시간 제한1초메모리 제한2048 MB

요약
IP 주소와 프리픽스 길이로 이루어진 라우터 테이블이 주어질 때, 각 질의 IP마다 가장 긴 프리픽스가 일치하는 항목의 번호를 출력하고, 일치하는 항목이 없으면 -1을 출력한다.
난이도

보통10점 중 4점

유형
문자열, 비트 연산, 구현, 완전 탐색
정답자
아직 제출이 없습니다

문제

Jayden is taking Computer Networks this semester and is currently learning about routers and IP address forwarding. His homework asks him to implement a technique called longest prefix matching to determine which entry in a router's lookup table corresponds to a given IP address. However, he is struggling and needs your help!

The router lookup table consists of nn entries, each consisting of a standard IP address and a subnet mask. For example, an entry could be:

192.168.1.0/24192.168.1.0/24

In this example, 192.168.1.0192.168.1.0 is the IP address and 2424 is the mask. The binary representation of an IP address can be found by converting the four decimal numbers in the address to binary (base 22) and adding zeros to the left until the result is 88 digits long. For example, the above address would have the following binary representation:

1100000011000000.1010100010101000.0000000100000001.0000000000000000/2424

The mask is the number of bits starting from the left that we need to match. In this example, all of the bits after the 24th bit are not important and will always be 00, so we can ignore them:

1100000011000000.1010100010101000.0000000100000001.********

Now, to check an IP address against this entry, we just check if the first 24 bits match. If they match, this would result in a matching of length 24. When we check an IP address against the lookup table, we are looking for the index of the entry that provides the longest matching. Consider the table from sample input 1:

IndexAddress
11100000011000000.1010100010101000.0000000100000001.********
21100000011000000.1010100010101000.00110011****.********
31100000011000000.1010100010101000.0000000100000001.10011001****

The first address we have to match is 192.168.1.148192.168.1.148 (1100000011000000.1010100010101000.0000000100000001.1001010010010100).

  • The first entry matches with a length of 24, since the 24 bits not ignored are the same as the given address.
  • The second entry does not match; the bits in the third group (00110011****) are not the same as the given address.
  • The third entry matches with a length of 28, since the 28 bits not ignored are the same as the given address.

Both row 1 and row 3 match. However, we print 33 because row 3 has the longest match (length 28).

If an address does not match any entries in the lookup table, print −1-1.

입력

The first line of input contains two integers n,mn,m (1≤n,m≤1031 \leq n, m \leq 10^{3})---the number of entries in the lookup table and the number of IP addresses to match, respectively.

The next nn lines of input represent an entry in the lookup table, and will be of the format a.b.c.d/ea.b.c.d/e (0≤a,b,c,d≤255,0≤e≤320 \leq a,b,c,d \leq 255, 0 \leq e \leq 32). It is guaranteed that no two entries in the lookup table will match the same set of IP addresses.

The next mm lines of input represent an IP address to match. Each address will be of the format a.b.c.da.b.c.d (0≤a,b,c,d≤2550 \leq a,b,c,d \leq 255).

출력

For each of the mm IP addresses to match, print the index of the longest prefix matching in the lookup table, or print −1-1 if it does not match anything in the lookup table.

예제1

  1. 예제 1

    입력
    3 3
    192.168.1.0/24
    192.168.48.0/20
    192.168.1.144/28
    192.168.1.148
    192.168.1.80
    192.10.1.255
    
    예상 출력
    3
    1
    -1